Persist javascript variables between GET requests?

与世无争的帅哥 提交于 2019-12-31 03:15:12

问题


ASP .NET is allowed
Storing the values in hidden input fields is allowed
Query String is not allowed
POST request is not allowed

It is possible to store JS variables between GET requests ?
I want to reinitialize them on the client using ClientScript.RegisterStartupScript

Can I use cookies for this ?
Are there other posibilities?
Where cookies are stored when Request is made ?


回答1:


Can I use cookies for this ?

Yes, see this tutorial on using cookies in Javascript.

Are there other posibilities?

If you are not allowed to append anything the URL of your requests, I can't come up with any.

Where cookies are stored when Request is made ?

In the HTTP request header. The aforementioned tutorial will tell you how to read their values from Javascript. On the server side with ASP.Net, you can read cookie values using Request.Cookie["cookieName"] which returns an instance of HttpCookie.




回答2:


I wouldn't highly recommend this, but the other option is to alter the window.name property.

You can save some minor bits of data here, then retrieve them on the next page load.

Pros:

  • Quick-n-dirty, but works

Cons:

  • Messes up any window references for popups/child iframes
  • Since its a "hack", browser vendors may break this "feature" in the future

Of course if you can exclude all the old browsers, then use Global/Client Session Storage!




回答3:


At the moment using cookies is your best bet. You can serialize the JavaScript objects to strings, and unserialize them back into objects later. A good choice format is JSON, since it is a subset of JavaScript.

There is also storing objects in Flash. Storing in Google Gears. DomStorage

See this library that has an interface to each: http://pablotron.org/?cid=1557

If you are in control of all aspects of the page, then you can also wrap the page in a top level frame. Then only refresh the child frame. You can then store content in the parent frame.

You can see this used in sites like GMail, and others where the only thing that changes in the URL is outside the #.

You don't even have to change the URL, that part is just put in for Human Friendly URLs. (So you can actually copy and paste URLs as is).




回答4:


If the objects you're storing are big, or if you want to restore them precisely (which JSON certainly doesn't) then use http://rhaboo.org. Most libraries serialise large objects and arrays into a single localStorage entry, but that means decoding and re-encoding the whole thing at every little change, which means gradually worsening performance for loyal users. Rhaboo uses a separate LS entry for each terminal value in the object, and then adjusts or appends them individually, so you don't get that performance hit.

BTW, I wrote rhaboo.



来源:https://stackoverflow.com/questions/1253821/persist-javascript-variables-between-get-requests

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!