在 GET 请求之间保留 javascript 变量?

发布于 2024-08-01 13:54:02 字数 248 浏览 5 评论 0原文

允许 ASP .NET
允许将值存储在隐藏输入字段中
不允许使用查询字符串
不允许 POST 请求

可以在 GET 请求之间存储 JS 变量吗?
我想使用 ClientScript.RegisterStartupScript 在客户端上重新初始化它们

我可以为此使用 cookie 吗?
还有其他可能性吗?
发出请求时 cookie 存储在哪里?

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 ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

拒绝两难 2024-08-08 13:54:02

我可以为此使用 cookie 吗?

是的,请参阅在 Javascript 中使用 Cookie 的教程

还有其他的可能性吗?

如果不允许您在请求的 URL 中附加任何内容,我就无法提供任何内容。

发出请求时 cookie 存储在哪里?

在 HTTP 请求标头中。 上述教程将告诉您如何从 Javascript 读取它们的值。 在使用 ASP.Net 的服务器端,您可以使用 Request.Cookie["cookieName"] 读取 cookie 值,该值返回 HttpCookie

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.

忆依然 2024-08-08 13:54:02

目前,使用 cookie 是您最好的选择。 您可以将 JavaScript 对象序列化为字符串,并稍后将它们反序列化回对象。 JSON 是一个不错的选择格式,因为它是 JavaScript 的子集。

Flash中也有存储对象。
存储在 Google Gears 中。
DomStorage

请参阅这个库,其中每个库都有一个接口:
http://pablotron.org/?cid=1557

如果您可以控制页面,那么您还可以将页面包装在顶级框架中。 然后只刷新子框架。 然后您可以将内容存储在父框架中。

您可以在 GMail 等网站以及其他网站中看到这种用法,其中 URL 中唯一发生变化的是 # 之外的内容。

您甚至不必更改 URL,该部分只是为了人类友好的 URL 而添加的。 (因此您实际上可以按原样复制并粘贴 URL)。

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).

归属感 2024-08-08 13:54:02

我不会强烈推荐这个,但另一个选择是更改window.name属性。

可以在此处保存一些较小的数据,然后在下一个页面加载时检索它们。

优点:

  • 速度快,但有效

缺点:

  • 弄乱弹出窗口/子 iframe 的任何窗口引用
  • 由于它是“黑客”,浏览器供应商可能会破坏此“功能” ” 将来

当然如果你可以排除所有旧浏览器,那就使用 Global/客户端会话存储

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文