简体   繁体   English

C#MVC4中的全局javascript变量

[英]Global javascript variable in C# MVC4

I followed the answer here by defining the js variable ahead. 我跟着答案在这里通过提前定义JS变量。 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? 所以我想知道浏览时如何在多个页面上保存js变量? Do I need to use session variables, querystring in C#? 我是否需要在C#中使用会话变量,querystring? 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. 在客户端,您可以使用Cookie或查询字符串,也可以将该值保留在服务器端的会话变量中,或者将其以post数据的形式发送到服务器端,然后将其放在页面中,以便在客户端代码。

One way would be use local storage, a HTML5 feature. 一种方法是使用本地存储,即HTML5功能。

http://paperkilledrock.com/2010/05/html5-localstorage-part-one/ 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. 当然,这取决于您的客户以及您是否希望他们能够使用HTML5。

Otherwise Cookies are probably the best route. 否则,Cookie可能是最佳途径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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