来自远程源的JavaScript Store值

时间:2017-07-21 09:28:59

标签: javascript php html variables server

我有一个本地Web服务器,它只返回由PHP构成的网页上的值。 (当我运行脚本时,它返回一个从0到100的值)

例如。 php script

在我的本地客户端,我想存储此值,但我不确定如何。

我知道解决方案必须简单,但我的研究还没有找到它。

2 个答案:

答案 0 :(得分:0)

使用 localStorage sessionStorage (如果时间很短,直到浏览器关闭)

答案 1 :(得分:0)

如果您需要没有过期日期的商店数据。你可以使用localStorage

在localStorage中,当浏览器关闭时,数据不会被删除,并且将在第二天,一周或一年中可用。您可以通过localStorage.setItem(“key”,value)或localStorage.key = value;

调用远程服务器的值

你可以从下面的代码中获得想法:

<!DOCTYPE html>
<html>
<head>
<script>
function clickCounter() {
    if(typeof(Storage) !== "undefined") {
        if (localStorage.clickcount) {
            localStorage.clickcount = Number(localStorage.clickcount)+1;
        } else {
            localStorage.clickcount = 1;
        }
        document.getElementById("result").innerHTML = "You have clicked the button " + localStorage.clickcount + " time(s).";
    } else {
        document.getElementById("result").innerHTML = "Sorry, your browser does not support web storage...";
    }
}
</script>
</head>
<body>
<p><button onclick="clickCounter()" type="button">Click me!</button></p>
<div id="result"></div>
<p>Click the button to see the counter increase.</p>
<p>Close the browser tab (or window), and try again, and the counter will continue to count (is not reset).</p>
</body>
</html>