javascript 保存变量值并在页面刷新后检索它

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11740669/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 14:11:40  来源:igfitidea点击:

saving variable value and retrieve it after page refresh

javascriptajax

提问by Da Silva

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?

我有一个字符串值保存到一个变量中,我的网页在某个过程后自动重新加载..我需要知道即使在页面刷新后我是否可以获得存储在该变量中的值?

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

我使用 javascript 代码刷新我的网页 window.location.reload()

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

如果不是,如果我使用像 php 这样的服务器端脚本,它会起作用吗?

回答by Utkanos

JavaScript:

JavaScript:

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

1) localStorage(仅限 HTMl5 浏览器)- 您可以将其保存为页面本地存储限额的属性

2) save it in a cookie

2)将其保存在cookie中

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

3) 将变量附加到 URL 哈希,以便location.hash在刷新后可以检索

PHP

PHP

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

1) 将其保存为会话变量并在每次页面加载时通过 AJAX 检索它

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

2) 将其保存在 cookie 中(如果您要对其进行 cookie,也可以采用 JS 方法)

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.

任何 PHP 方法都会很笨拙,因为您必须首先通过 AJAX 将变量的值发送到 PHP 脚本,然后在重新加载后通过 AJAX 检索它。

回答by Matt

You can store this as a $_SESSIONvariable.

您可以将其存储为$_SESSION变量。

session_start();

$myVar = null;

// some code here

if (!isset($_SESSION['myVar'])) {
    $_SESSION['myVar'] = "whatever";
} else {
    $myVar = $_SESSION['myVar'];
}

回答by madflow

You will have to persist this variable in a Cookie/Webstorage/Session. Web pages are stateless.

您必须将此变量保存在 Cookie/Webstorage/Session 中。网页是无状态的。

回答by Fabrizio Calderan

yes you could save your variable on server side as sessionvalue or on client side in localstorage(or as a cookie)

是的,您可以将服务器端的变量保存为session值或在客户端localstorage(或作为cookie

回答by Danny Beckett

Cookies are your friend:

饼干是你的朋友:

// Set a cookie or 2
document.cookie = 'somevar=somevalue';
document.cookie = 'another=123';

function getCookie(name)
{
    // Cookies seperated by ;   Key->value seperated by =
    for(var i = 0; pair = document.cookie.split("; ")[i].split("="); i++)
        if(pair[0] == name)
            return unescape(pair[1]);

    // A cookie with the requested name does not exist
    return null;
}

// To get the value
alert(getCookie('somevar'));

回答by dmmd

Using javascript you can store that variable into a cookie (the user must have cookies enabled), and then retrieve the cookie after.

使用 javascript,您可以将该变量存储到 cookie 中(用户必须启用 cookie),然后检索 cookie。

The approach is something like this:

方法是这样的:

To save:

保存

function setCookie(c_name,value)
{
var exdate=new Date();
exdate.setDate(exdate.getDate() + 1);
var c_value=escape(value);
document.cookie=c_name + "=" + c_value;
}

To retrieve:

检索

function getCookie(c_name)
{
var i,x,y,ARRcookies=document.cookie.split(";");
for (i=0;i<ARRcookies.length;i++)
{
  x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
  y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
  x=x.replace(/^\s+|\s+$/g,"");
  if (x==c_name)
    {
    return unescape(y);
    }
  }
}

Ref: http://www.w3schools.com/js/js_cookies.asp

参考:http: //www.w3schools.com/js/js_cookies.asp