【问题标题】:JavaScript Store Value from remote source来自远程源的 JavaScript 存储值
【发布时间】:2017-07-21 09:28:59
【问题描述】:

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

例如。

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

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

【问题讨论】:

  • 如果 Javascript 全局变量不起作用,您可以使用 localStorage 来满足要求。

标签: javascript php html variables server


【解决方案1】:

使用 localStoragesessionStorage(如果时间跨度很短,即直到浏览器关闭)

【讨论】:

    【解决方案2】:

    如果您需要没有过期日期的存储数据。你可以使用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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-09-07
      • 2015-07-03
      • 1970-01-01
      • 2011-12-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-14
      相关资源
      最近更新 更多