如何在HTML文件之间保留变量值?

时间:2021-03-20 16:49:27

Let's say I have a page that refers to a .js file. In that file I have the following code that sets the value of a variable:

假设我有一个引用.js文件的页面。在该文件中,我有以下代码来设置变量的值:

var foo;

function bar()
    {
    foo = //some value generated by some type of user input
    }

bar();

Now I'd like to be able to navigate to another page that refers to the same script, and have this variable retain the value set by bar(). What's the best way to transport the value of this variable, assuming the script will be running anew once I arrive on the next page?

现在,我希望能够导航到另一个引用相同脚本的页面,并使此变量保留bar()设置的值。什么是传输此变量值的最佳方法,假设一旦到达下一页,脚本将重新运行?

4 个解决方案

#1


5  

You can use cookies.

你可以使用cookies。

Cookies were originally invented by Netscape to give 'memory' to web servers and browsers. The HTTP protocol, which arranges for the transfer of web pages to your browser and browser requests for pages to servers, is state-less, which means that once the server has sent a page to a browser requesting it, it doesn't remember a thing about it. So if you come to the same web page a second, third, hundredth or millionth time, the server once again considers it the very first time you ever came there.

Cookie最初是由Netscape发明的,用于为Web服务器和浏览器提供“内存”。安排将网页传输到浏览器的HTTP协议和浏览器对页面到服务器的请求是无状态的,这意味着一旦服务器将页面发送到请求它的浏览器,它就不记得关于它的事情。因此,如果您第二次,第三次,第100次或第一百次访问同一网页,服务器将再次认为这是您第一次来到这里。

This can be annoying in a number of ways. The server cannot remember if you identified yourself when you want to access protected pages, it cannot remember your user preferences, it cannot remember anything. As soon as personalization was invented, this became a major problem.

这可能在许多方面令人讨厌。服务器无法记住,当您想要访问受保护的页面时,您是否已经确定了自己,它无法记住您的用户偏好,也无法记住任何内容。一旦个性化被发明,这就成了一个主要问题。

Cookies were invented to solve this problem. There are other ways to solve it, but cookies are easy to maintain and very versatile.

发明饼干是为了解决这个问题。还有其他方法可以解决它,但cookie易于维护且功能多样。

See: http://www.quirksmode.org/js/cookies.html

#2


3  

You can pass the value in the query string.
When the user navigate to the other page append the value to the query string and load it in the next.

您可以在查询字符串中传递值。当用户导航到另一页时,将值附加到查询字符串并在下一页中加载它。

#3


1  

Another option is jStorage. jStorage is probably better used for cached data and lossy user preferences (e.g. saved username in a login form), as it doesn't have full browser support (but IE6+ and most other common browsers support it) and cannot be relied upon (like cookies).

另一种选择是jStorage。 jStorage可能更适合用于缓存数据和有损用户首选项(例如,在登录表单中保存用户名),因为它没有完整的浏览器支持(但IE6 +和大多数其他常见浏览器都支持它)并且不能依赖(如cookie) )。

#4


0  

You can use YUI's Cookie Library http://developer.yahoo.com/yui/cookie/

您可以使用YUI的Cookie库http://developer.yahoo.com/yui/cookie/

#1


5  

You can use cookies.

你可以使用cookies。

Cookies were originally invented by Netscape to give 'memory' to web servers and browsers. The HTTP protocol, which arranges for the transfer of web pages to your browser and browser requests for pages to servers, is state-less, which means that once the server has sent a page to a browser requesting it, it doesn't remember a thing about it. So if you come to the same web page a second, third, hundredth or millionth time, the server once again considers it the very first time you ever came there.

Cookie最初是由Netscape发明的,用于为Web服务器和浏览器提供“内存”。安排将网页传输到浏览器的HTTP协议和浏览器对页面到服务器的请求是无状态的,这意味着一旦服务器将页面发送到请求它的浏览器,它就不记得关于它的事情。因此,如果您第二次,第三次,第100次或第一百次访问同一网页,服务器将再次认为这是您第一次来到这里。

This can be annoying in a number of ways. The server cannot remember if you identified yourself when you want to access protected pages, it cannot remember your user preferences, it cannot remember anything. As soon as personalization was invented, this became a major problem.

这可能在许多方面令人讨厌。服务器无法记住,当您想要访问受保护的页面时,您是否已经确定了自己,它无法记住您的用户偏好,也无法记住任何内容。一旦个性化被发明,这就成了一个主要问题。

Cookies were invented to solve this problem. There are other ways to solve it, but cookies are easy to maintain and very versatile.

发明饼干是为了解决这个问题。还有其他方法可以解决它,但cookie易于维护且功能多样。

See: http://www.quirksmode.org/js/cookies.html

#2


3  

You can pass the value in the query string.
When the user navigate to the other page append the value to the query string and load it in the next.

您可以在查询字符串中传递值。当用户导航到另一页时,将值附加到查询字符串并在下一页中加载它。

#3


1  

Another option is jStorage. jStorage is probably better used for cached data and lossy user preferences (e.g. saved username in a login form), as it doesn't have full browser support (but IE6+ and most other common browsers support it) and cannot be relied upon (like cookies).

另一种选择是jStorage。 jStorage可能更适合用于缓存数据和有损用户首选项(例如,在登录表单中保存用户名),因为它没有完整的浏览器支持(但IE6 +和大多数其他常见浏览器都支持它)并且不能依赖(如cookie) )。

#4


0  

You can use YUI's Cookie Library http://developer.yahoo.com/yui/cookie/

您可以使用YUI的Cookie库http://developer.yahoo.com/yui/cookie/