jquery - How to access session variable from JavaScript file (.js) file? - Stack Overflow

I have to access session variable from javascript fileI am using below code to access from javascript,v

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
Add a ment  | 

2 Answers 2

Reset to default 1

i 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条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信