在nodejs

时间:2016-12-25 11:27:05

标签: javascript node.js

我是node.js的新手。 我有两个html文件和一个共享common.js。 在index1.html中,当单击锚点按钮时,它调用common.js的changetest()函数(更新测试变量)。 现在我想在index2.html中使用更新的变量。 但是index2.html选择了test的原始值。 (这可能是因为在新页面index2.html重新加载common.js) 请帮助在index2.html中传递和接收测试值的好方法。

Index1.html

<html>
    <head>
        <script type="text/javascript" src="common.js"></script>
    </head>
    <body>
        <h1>Sample Page</h1>
        <a href="index2.html" onclick="changetest()">Navigate</a>
    </body>
</html>

common.js

var test="abc";

function changetest(){
    test = "xyz";
}

index2.html

<html>
    <head>
        <script type="text/javascript" src="common.js"></script>
    </head>
    <body>
        <h1>Sample Page2</h1>
        <a href="#"><script>document.write(test)</script></a>
    </body>
</html>

1 个答案:

答案 0 :(得分:0)

我建议使用https://developer.mozilla.org/en/docs/Web/API/Window/localStorage

有了它,您可以轻松地从用户浏览器设置和获取数据。

使用它就像这样做

localStorage.setItem('test', 'My test value');

alert( "test= " + localStorage.getItem("test"));

https://developer.mozilla.org/en-US/docs/Web/API/Storage/LocalStorage

希望这有帮助。