简体   繁体   中英

Global javascript variable in C# MVC4

I followed the answer here by defining the js variable ahead. However the scope of that variable doesn't stay if I go to another page when browsing. Which seems reasonable since I do go to another page in the browser. So I wonder how can I save the js variable across multiple pages when I'm browsing? Do I need to use session variables, querystring in C#? Or is there any better solution?

There are no variables with a scope across pages. On the client side you can use cookies or querystrings, or you can keep the value in a session variable on the server side, or send it in post data to the server side, and put it in the page so that it is available in the client side code.

One way would be use local storage, a HTML5 feature.

http://paperkilledrock.com/2010/05/html5-localstorage-part-one/

Of course, it depends on your clients and whether you expect them to be able to use HTML5.

Otherwise Cookies are probably the best route.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM