JavaScript - how to get Session value in external javascript file
Asked By Gopal M on 01-Dec-08 01:22 AM
how to get Session value in external javascript file...
can anybody help me...
Thanks in advance.
Regards
Gopal M
Use the <%%> tag
[)ia6l0 iii replied to Gopal M on 01-Dec-08 01:25 AM
var sessionvalue = "<%=sessionVariable%>";
Use the <%%> tag
[)ia6l0 iii replied to Gopal M on 01-Dec-08 01:26 AM
var sessionvalue =
"'<%'=sessionvariable'%>'";
Remove the single quotes
Use :: var sessionvalue = <%"=sessionvariablename"%>
[)ia6l0 iii replied to Gopal M on 01-Dec-08 01:27 AM
Setting Session Variable From Javascript
Binny ch replied to Gopal M on 01-Dec-08 01:29 AM
there are really only two ways, rename your javascript file .aspx and then
you can treat it as a server-side aspx file that outputs client-side
javascirpt.
or declare a variable in your main page before you include this file,
something like:
<script language="JavaScript">
var sessionValue = '<%=(string)Session["whatever"]%>';
</script>
<script scr="....."....></script>
which makes "sessionValue" a global on your page and in your script.
Re :: Get Session Value in external Javascript file
Shailendrasinh Parmar replied to Gopal M on 01-Dec-08 01:30 AM
You
can do this in javascript, but you will
not be able to put the javascript in an external (*.js) file.
Put the following code in your <head> section and it will work:
<script type="text/javascript" language="javascript">
window.onload = function() {
var a = '<%=Session["lon"]%>';
alert(a);
}
</script> Hope this helps.
Re :: Get Session Value in external Javascript file
Shailendrasinh Parmar replied to Gopal M on 01-Dec-08 01:32 AM
Keep your session value in some variable of type protected in code behind like string or Hiddenfields
like : protected string sUserName = Session("UserName").ToString();
Now you can access this variable in HTML code and in javascript like
<script language=javascript>
var sUser = "<%=sUserName%>";
</script>
Hope this helps.
use this
Kalit Sikka replied to Gopal M on 01-Dec-08 01:34 AM
I have an asp button click event that sets:
Session["lat"] = txtbLat.Text.ToString(); I then want to read that session value in JavaScript:
var a = '<%=Session["lon"]%>';
alert(a);
try this
C_A P replied to Gopal M on 01-Dec-08 01:49 AM
No, not directly. Your session vars are controlled by the server,
JS runs client side, and as such cannot modify the vars directly.
You need to make server requests using POST or GET and hidden
iframes, or XMLHTTPRequest() calls to send data from the JS to
the server, and then have your ASP.NET code handle the vars.
try this code
C_A P replied to Gopal M on 01-Dec-08 01:51 AM
code...
protected void Page_Load(object sender, EventArgs e)
{
if (Convert.ToString(Session["Total"]) == string.Empty)
{
pllogin.Visible = true;
plbasket.Visible = false;
}
else
{
pllogin.Visible = false;
plbasket.Visible = true;
lblquantity.Text = Convert.ToString(Session["TotQty"]);
lblprice.Text = Convert.ToString(Session["Total"]);
}
}
To rule it out, try adding a random query
parameter to the url you navigate to in javascript.
source from
http://www.velocityreviews.com/forums/t301773-session-values-and-javascript.html
check this
sri sri replied to Gopal M on 01-Dec-08 03:56 AM
hi,
you cant use the below tag
<%=sessionVariable%>
in external js.
put the session value in the hidden field and read the hidden field in your js file.
//aspx page
<asp:HiddenField ID="hidText" runat="server" />
//code behind
protected void Page_Load(object sender, EventArgs e)
{
hidText.Value = Convert.ToString(Session.SessionID);
}
js
document.getElementById("ctl00_BodyContentPH_hdf_hidText").value
Assign session variable in script block, consume in external JavaScript
Eric Eggers replied to sri sri on 27-Jan-10 11:03 AM
I have had success with assigning the session variable in a script block on the page
markup, then using that variable in the external JavaScript source file. Semi-psuedocode
from the page and JS source follows, respectively:
In page markup:
...
<body ...>
<script type='text/javascript'>
var sessionVarForJS = "<%=
sessionVariable %>";
</script>
<script type='text/javascript' src='./js/externalJS.js'></script>
...
In externalJS.js:
...
if (sessionVarForJS == "somethingDesired")
doSomething("whatever");
...
Riley K replied to Gopal M on 14-Apr-11 07:39 AM
Use this Code
<script type="text/javascript" language="javascript">
var sessionValue = '<%= Session["SesValue"] %>';
alert(sessionValue);
</script>