views:

274

answers:

3

Hi,

I have a code like

var mySession = "<%= Session["MyID"] %>";
alert ( mySession );

and when I place this code in my aspx page inside a script tag, it works fine. But it doesn't work in an external js file. I am not able to figure out the problem.

Is there anything wrong in my code, or is it like this by default?

Thanks in advance

+4  A: 

Server side scripts (<%= %>) are evaluated only inside aspx pages. External javascript are static files. To achieve what you are looking for you might need to declare a global js variable inside your aspx file:

var mySession = "<%= Session["MyID"] %>";

and use it in your external js:

alert(mySession);

Another option is to use AJAX. Set up a server side script which will return the required session value and call this script from the external js file.

Darin Dimitrov
If I declare a global variable and set the value on page load, I won't get the updated session value on click events inside the js file. Is there anything I can do to get this working using your first approach?
rahul
How is the session updated on click events while you are only inside the client script? Are you using UpdatePanels sending AJAX requests to your server?
Darin Dimitrov
No, my session times out after a specified time, and I need to check that also. So when my page loads there will be value in session and when the session expires and the user clicks on a button I need to get the updated session and check that against null and do the appropriate action.
rahul
What happens when the user clicks on the button? Do you perform a Postback? If yes then the page is refreshed and your global variable will be updated with the respective value of the session.
Darin Dimitrov
No, there won't be any postback. A popup window will be opened and the user will be prompted to re login.
rahul
You could set up a generic handler (`.ashx`) which returns javascript. Then in your page you include this dynamic javascript function: `<script type="text/javascript" src="/mydynamichandler.ashx"></script>`
Darin Dimitrov
+1  A: 

Darin's suggestion is best, but if for someone reason you don't want to use the convention of passing data into your external js code by means of variables defined on the aspx page, you could actually make your external js file an aspx page. For example, you could name it "External.js.aspx" and set ContentType="text/javascript" in the @Page directive. Then you can do everything you expect to be able to do with ASP.NET from inside the javascript source.

However, serving aspx pages is much more computationally expensive than having IIS serve a static file. If that is not a concern for you, then this technique could provide a more maintainable and rapid approach.

gWiz
A: 

A nice way is to add all the values you need in the external Js files is to have an input type="hidden" for every value you need from aspx => js.

And on javascript window onload have a method that finds all you input fields and store them in an JS object.

fredrik