Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing Asp.Net Session variables in JS

Tags:

javascript

c#

I'm not able to access the variable in my .js file

the code i have at the top of the page is:

<script type="text/javascript">
    privilage = '<%=Session["privilage"]%>';
</script>

then i want to access privilage in my .js file.

i just want to alert this at the moment. would i be able to do this?

thanks

like image 304
Ksnrg Avatar asked Jan 08 '13 11:01

Ksnrg


2 Answers

You have to store session Value in HiddenField. After that You can Access the HiddneFieldValue in your JS

 <script type="text/javascript">
     document.getElementById('hdnField').value = '<%=Session["privilage"]%>';
 </script> 
like image 177
Aarif Qureshi Avatar answered Oct 09 '22 01:10

Aarif Qureshi


Just use:

var privilage = '<%=Session["privilage"]%>';

or try:

alert(privilage);

It will display your Session value

like image 29
Renjith JR Avatar answered Oct 09 '22 01:10

Renjith JR