I have to access session variable from javascript
file
I am using below code to access from javascript
,
var sessionValue = '<%=Session["username"]%>'
alert(sessionValue)
it is working well when the script function is placed inside the aspx page
...but when I use the same code in .js
file it is not working
I have seen all the related answers, nothing is useful for me, so don't mark it as duplicate
I have to access session variable from javascript
file
I am using below code to access from javascript
,
var sessionValue = '<%=Session["username"]%>'
alert(sessionValue)
it is working well when the script function is placed inside the aspx page
...but when I use the same code in .js
file it is not working
I have seen all the related answers, nothing is useful for me, so don't mark it as duplicate
Share Improve this question edited Dec 27, 2013 at 13:41 Yagnesh Agola 4,6556 gold badges38 silver badges50 bronze badges asked Dec 27, 2013 at 13:27 Kevin MKevin M 5,5064 gold badges46 silver badges46 bronze badges 4-
because
Session
is not available in.js
file. It can be access from your apsx page only. – Yagnesh Agola Commented Dec 27, 2013 at 13:30 - This should help stackoverflow./questions/1343801/… – geedubb Commented Dec 27, 2013 at 13:30
-
@Shahe actually you could write an
HTTPHandler
which will dynamically write the .js file – geedubb Commented Dec 27, 2013 at 13:31 - Use ajax request from js file to load session variable. – dima-hx Commented Dec 27, 2013 at 13:32
2 Answers
Reset to default 1i think try to write your session value to a hidden html element and read value of this hidden element with javascript as follow :
<input type="hidden" id="session" value="'<%=Session["username"]%>'">
at your js:
var sessionValue =document.getElementById("session").value;
Depends on what is your scripting languauge in server side, if it is JSP or PHP following should work.
var sessionValue = "'"+<%=Session["username"]%>+"'"
alert(sessionValue)
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745382831a4625308.html
评论列表(0条)