-1

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

Yagnesh Agola
  • 4,556
  • 6
  • 37
  • 50
Kevin M
  • 5,436
  • 4
  • 44
  • 46

2 Answers2

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;
semirturgay
  • 4,151
  • 3
  • 30
  • 50
1

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)
WebServer
  • 1,316
  • 8
  • 12