Connecting Tech Pros Worldwide Forums | Help | Site Map

Passing variables between Javascript and Visual Basic

Member
 
Join Date: May 2008
Posts: 44
#1: Nov 28 '08
When a variable is created using Javascript, should it then be able to be read immediately using embedded VB code or does the submit command have to be entered or are the variables between javascript, visual basic, and vbscript non-interchangable?

I created the following code:

Expand|Select|Wrap|Line Numbers
  1. <script language="javascript">
  2. function question(){
  3.     strReply = confirm('Click Yes if you wish to continue.')
  4.     alert(strreply)
  5. }
  6. </script>
I would then like to take action based on the user response in the VB code.

<%
Test value of strReply and perform code based on the response
%>

The problem is, that although the reply variable can be read with the alert command in the Javascript section, it cannot be read in the VB code. I tried creating a reply input field:

<input id="reply" name="reply" type="text" value="<%=strReply%>"/>

and then in the Javascript section, entering the following command:

document.frmKPIActMaintLoc_01.reply.value = strReply

but it also did not work.

acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#2: Nov 28 '08

re: Passing variables between Javascript and Visual Basic


Is this Visual Basic (on the server) or vbscript (client)?

Note that interaction between JavaScript and VBScript will only work in IE. If, however, you mean VB then you should be able to pass it to the server-side script using a form, via the URL, via cookies or via Ajax.

PS. please use [code] tags when posting code. Thanks!
Member
 
Join Date: May 2008
Posts: 44
#3: Nov 28 '08

re: Passing variables between Javascript and Visual Basic


Thanks for the response. Do you have a code example of passing the variable between JavaScript and VB. The input field, I gave in my example was within a form and I tried to set the input field within the JavaScript as listed in the example, but I must be missing something, because the variable remained blank.
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#4: Nov 29 '08

re: Passing variables between Javascript and Visual Basic


If you want VB code to read a JavaScript variable, then you will need to pass that value to the server-side page. You can use a form and submit it, e.g.
Expand|Select|Wrap|Line Numbers
  1. <input type="hidden" id="reply" name="reply" value="">
then in javascript:
Expand|Select|Wrap|Line Numbers
  1. function question(){
  2.     strReply = confirm('Click Yes if you wish to continue.')
  3.     document.getElementById("reply").value = strReply;
  4. }
Now this value will be available on the submitted page once the form is submitted. There are other ways too.
Member
 
Join Date: May 2008
Posts: 44
#5: Nov 30 '08

re: Passing variables between Javascript and Visual Basic


acoder,
Thanks for the response. I put in the code in a test page and everything seems to be working. I then changed to VBScript, to use a different message and everthing worked fine. When I moved the code to the actual page where it will be used, it seemed that it is being caught in a loop and keeps asking the question: Do you wish to continue? Are you aware of any quirks when embedding the submit command in the code, when combined with other code, which would cause it to produce and infinite loop. I use an If statement to check if variable strDelRun is set to 1, and within the code, it is set to 2, so it should not be executed the second time. On the test page it seems to work fine.

Thanks for the help,
Ralph

Expand|Select|Wrap|Line Numbers
  1. If strUpdateMode2 = "D" And strDelRun = "1"  Then
  2. %>
  3.     <script language="VBscript">
  4.                 strDelRet = MsgBox("Do you wish to continue? Y/N",68,"KPI Analysis")
  5.                 document.getElementById("DelRet").value = strDelRet
  6.                  document.getElementById("DelRun").value = "2"
  7.             </script> 
  8.  <%           
  9.              strDelRun = "2"  
  10.  %>            
  11.    <script language="JavaScript">
  12.      document.frmKPIActMaintLoc_01.submit()          
  13.    </script>               
  14. <%             
  15. End IF
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#6: Nov 30 '08

re: Passing variables between Javascript and Visual Basic


It doesn't work quite like that. JavaScript/VBScript is client-side and VB (ASP) is server-side. Once the page has loaded, the server-side code will not change. To effect some change server-side using client-side code, you either need to use Ajax or submit the page.

Can you post all of the code that doesn't work.
Member
 
Join Date: May 2008
Posts: 44
#7: Dec 2 '08

re: Passing variables between Javascript and Visual Basic


It's too long to post here. Is there an email address I could send it to?
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#8: Dec 3 '08

re: Passing variables between Javascript and Visual Basic


Either attach it to your post, post a link to a test page or PM me.
Reply


Similar JavaScript / Ajax / DHTML bytes