We help IT Professionals succeed at work.

Using Session Variable in JavaScript

OB1Canobie
OB1Canobie asked
on
I have a web page for asp.net vb. I have a session variable "ProjectID" that I need to access for a path in JavaScript. Can someone assist in how to access the session variable in the building of the path. I am assigning a variable the path. See what I have thus far. I need the session variable value as indicated. Thanks.

var newhref = ("../forms/form_task_utility.aspx?Mode=A%ProjectID="  %>);
Comment
Watch Question

Javascript runs at client side, hence to use session variables, you have to create a variable in javascript for use in client side.

try the following..

var pid = '<% projectId %>';
var newhref = ("../forms/form_task_utility.aspx?Mode=A&ProjectID="+pid);


IMP: not sure how to print in asp.net vb,.. replace <% projectId %> with appropriate print statement...

BR:Sudhindra:www.clicksperday.com
Another way I've used is to have a hidden field on the form with a runat="server" in it, and set the value in the code behind.  I try to avoid in-line code and keep my .Net code separated in the code behind as much as possible. Then to retrieve, just get the value in the js:

pID = document.getElementById("hProjID").value;
var newhref = ("../forms/form_task_utility.aspx?Mode=A&ProjectID=" + pID);

-MJC

Explore More ContentExplore courses, solutions, and other research materials related to this topic.