节能变量的值,并刷新页面后找回变量、节能、页面

2023-09-10 17:39:19 作者:誰以嬌顏、亂了紅塵

我必须保存在一个变量的字符串值,我的网页自动重新加载一定的过程以后。我需要知道,如果我可以,即使刷新页面获取存储在该变量的值?

I have a string value saved in to a variable, my webpage auto reloads after a certain process.. I need to know if I can get the value stored in that variable even after page refresh?

林清爽的使用javascript code我的网页 window.location.reload()

Im refreshing my web page using javascript code window.location.reload()

如果不将它的工作,如果我把服务器端脚本,如PHP?

If not will it work if I take to server side script like php?

推荐答案

JavaScript的:

1)的localStorage (HTML5的浏览器只) - 你可以将其保存为网页的本地存储津贴的属性

1) localStorage (HTMl5 browsers only) - you could save it as a property of the page's local storage allowance

2),将其保存在cookie

2) save it in a cookie

3)刷新后追加变量到URL哈希所以它的检索通过的location.hash

3) append the variable to the URL hash so it's retrievable via location.hash after the refresh

PHP

1),将其保存为一个会话变量,每一次检索它在AJAX页面加载

1) save it as a session variable and retrieve it over AJAX each time the page loads

2)保存在一个cookie(还不如做JS的方法,如果你要COOKIE吧)

2) save it in a cookie (might as well do the JS approach if you're going to cookie it)

任何PHP的做法是笨重,你不得不重装后的第一次发送的变量的值超过AJAX PHP脚本,然后检索它在AJAX。

Any PHP approach would be clunky as you'd have to first send the value of the variable to a PHP script over AJAX, then retrieve it over AJAX after reload.