Passing Asp Session Variable To Javascript Function

They have: 12 posts

Joined: Mar 2002

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's picture

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's picture

They have: 571 posts

Joined: Feb 2001

I actually spent pretty much a whole day trying to do something similar once!

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's picture

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's picture

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. Wink

dk01's picture

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.