0

I'm new to node.js. I have two html files and one shared common.js. In index1.html, when anchor button is clicked it calls changetest() function of common.js(which updates test variable). now i want to use the updated variable in index2.html. But index2.html picks the original value of test. (which may be because in new page index2.html reloads the common.js) please help what can be the good way of passing and receiving test value in 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>
shail
  • 13
  • 2

1 Answers1

0

I would reccomend use https://developer.mozilla.org/en/docs/Web/API/Window/localStorage

With it you can easily set and get data from user browser.

To use it just do like this

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

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

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

Hope this helps.

Mykola Borysyuk
  • 3,373
  • 1
  • 18
  • 24
  • Thanks for the suggestion. but i'm worried if user disables the localstorage support. – shail Dec 25 '16 at 13:15
  • https://blog.yell.com/2016/04/just-many-web-users-disable-cookies-javascript/ Well in that case you have to worry about user disable JS and other stuff. It's highly unlikely that you have users with this option disabled. – Mykola Borysyuk Dec 26 '16 at 21:19