Click to See Complete Forum and Search --> : Converting a Javascript variable to ASP variable.
raktims
01-31-2003, 10:31 AM
Hi,
I saw a post on converting a asp variable to Javascript. How can I do the opposite - converting Javascript variable to Asp variable ?
Thanks,
Raktims
khaki
01-31-2003, 11:17 AM
I bookmarked that one for myself.
Dave Clark (Certified Master) and Swon (Supreme Master.... hey wait a minute. ANOTHER Supreme Master of the Web? Does jpmoriarty know about this?!) answered it in the JavaScript forum last month.
Here it is:
http://forums.webdeveloper.com/showthread.php?s=&threadid=1232
I've also done it by populating a hidden text box with the variable and then capturing it that way - because that converts it into an OBJECT or something :rolleyes: , but that's probably just a workaround that I'll get chastized for suggesting (wink).
Go ahead Charles, or Dave or...(?). I know it's coming (lol)!
Anyway, the link should help do what you are looking to do.
k
Ribeyed
01-31-2003, 12:01 PM
Hi,
couple of ways you can do this:
<script language=javascript>
var Test = <%=ASPVariable%>
</script>
this would work or something like this would also work:
<%
dim serverVar
serverVar = request("testvar")
response.write("<html>" & vbcrlf)
response.write("<head>" & vbcrlf)
response.write("<script>" & vbcrlf)
response.write("function popUp() {" & vbcrlf)
response.write(" var clientVar = " & serverVar & ";" & vbcrlf)
response.write(" alert(clientVar);" & vbcrlf)
response.write("}" & vbcrlf)
response.write("</sciprt>" & vbcrlf)
response.write("</head>" & vbcrlf)
response.write("<body onLoad=""popUp()"">" & vbcrlf)
response.write("The variable value is in the popup window.")
response.write("</body>" & vbcrlf)
response.write("</html>")
%>
Hope this helps.
raktims
01-31-2003, 02:06 PM
Thanks, but I wanted to do just the opposite:
Transfering the value of a Javascript Var to a ASP var like :
JavaS_Var = ASP_Var
Raktims
Ribeyed
01-31-2003, 02:25 PM
hi,
as ASP is server side and JavaScript is client side then the ASP is run first on the server and then the JavaScript is run at the browser, it would then involve another round trip to the server to change the JavaScript variable to a ASP variable. Surely this is not what you are trying to do? If it is then why would you need to do this? Maybe there is a better solution to your problem if you would like to post what it is your trying to acomplish.
Hope this helps
khaki
01-31-2003, 02:52 PM
OK, now I'm confused (alert the media!).
You say that you want to "transfer the value of a Javascript Var to a ASP var"
Then you write:
JavaS_Var = ASP_Var
But that assigns the ASP variable's value to the JavaScript variable (opposite right? I'm feeling woozy).
So, unless you are going to pass the variable to a new page, I don't understand why you would need to share the value of the variable between JavaScript and ASP after the page has already been written (although you may have your reasons, and just because I don't understand what that reason is, does not mean that you don't have a darn good reason!).
I guess I'm with [SWR]Ribeyed. If you can state what your trying to accomplish, then maybe someone can provide you with the best appraoch for handling it (or something like that. I think that I'm having a spell).
spinning....
k
Can anyone give me a little detail about this. I'm a newbie, and I'm having the same problem... I think.
I've tried SWON's solution from the link above. It works but then spins out of control appending ?a= over and over and over....
Would the hidden form be better for me?
I'd really appreciate any help you may have to offer.
<% @LANGUAGE="VBSCRIPT" %>
<%
Option Explicit
Response.Buffer = True
On Error Resume Next
Dim oConn, oRS, oSQL, oStores, oFranchise
Set oConn = Server.CreateObject("ADODB.Connection")
Set oRS = Server.CreateObject("ADODB.Recordset")
oStores = Request.Form("oStores")
oFranchise = Request.Form("oFranchise")
%>
<HTML>
<HEAD>
<TITLE>Test 1</TITLE>
</HEAD>
<BODY link='navy' alink='red' vlink='navy'>
<SCRIPT LANGUAGE="JavaScript">
<!--
var Stores="<%Response.Write(oStores)%>";
var array = Stores.split(', ');
var temp = array.join("','");
var StoreString = ("'"+temp+"'");
//-->
</SCRIPT>
<%
oConn.ConnectionString = "DSN=xxxx;UID=xxxx;PWD=xxxx;"
OConn.Open
oSQL = "SELECT COUNT(blah) AS Returned FROM blahtable WHERE blahvalue IN ("
oSQL = oSQL & StoreString & ")"
oRS.Open oSQL, oConn
Response.Write(oSQL)
%>
HTML etc...
Bullschmidt
01-16-2006, 04:10 PM
Hi and welcome to the board ck1!
What you are doing with this looks fine:
var Stores="<%Response.Write(oStores)%>";
Just a matter of developer preference but I might code is like this with the same result:
var Stores = "<%= (oStores) %>";
And when you do a right-click | View Source you should be able to see something more like this showing the actual value of the oStores ASP variable:
var Store="235";