Passing Asp Session Variable To Javascript Function
I'm pulling my hair out trying to figure out how to pass an asp session variable to a local variable withing a javascript function. Something like this:
var zip =
<?php
=Session("clzip")
?>
but it's the quotes around "clzip" that is throwing a syntax error I think. I've tried single quotes and no quotes. Can't get it to work. Anyone have any ideas?
Abhishek Reddy posted this at 02:23 — 16th April 2002.
He has: 3,348 posts
Joined: Jul 2001
There aren't escape characters for quotes in ASP so try this:
Response.Write("var zip = " & chr(34) & Session("clzip") & chr(34) & ";" & vbCrLf)
Which will output:
var zip = "YourVariableValue";
detox posted this at 13:59 — 16th April 2002.
They have: 571 posts
Joined: Feb 2001
I actually spent pretty much a whole day trying to do something similar once!
Angela French posted this at 16:03 — 16th April 2002.
They have: 12 posts
Joined: Mar 2002
But response.write is vbscript. I'm trying to pass the session variable to a new variable inside a javascript function.
dk01 posted this at 20:06 — 16th April 2002.
He has: 516 posts
Joined: Mar 2002
Assuming clzip is equal to the string "hello" then this
<script language="Javascript">
myvar =
<?php
=Session("clzip")
?>
</script>
would return
<script language="Javascript">
myvar = hello;
</script>
However you need to assign the string to a string in javascript so you need to put quotes around it like so:
<script language="Javascript">
myvar = "
<?php
=Session("clzip")
?>
</script>
and it should give back:
<script language="Javascript">
myvar = "hello";
</script>
if your session var is a number or is boleen then you might need to use it without the quotes. if you are already doing that then you should get it to work. hope this helps!
-dk
Abhishek Reddy posted this at 01:45 — 17th April 2002.
He has: 3,348 posts
Joined: Jul 2001
Just looking at it again, I don't think it's just the quotes but also the missing semicolon that might've caused the syntax error.
dk01 posted this at 05:22 — 17th April 2002.
He has: 516 posts
Joined: Mar 2002
oops. i overlooked that too. good one.
-dk
Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.