2

I have shared js code like this in angus.js

var g_colour;

function getcolour() {
  return g_colour;
}

function setcolour(colour) {
  g_colour = colour;
}

Which is accessed by html pages 1 and 2 like this:

1.html:

<html>
<head>
<title>Global javascript example</title>
</head>
<body>
<a href="2.html">Page2</a>
<script src="angus.js"></script>
<form name="frm">
<input type="button" value="Setblue" onclick="setcolour('blue');" />
<input type="button" value="Setyellow" onclick="setcolour('yellow');" />
<input type="button" value="getcolour" onclick="alert(getcolour());" />
</form>
</body>
</html>

2.html:

<html>
<head>
<title>Global javascript example page 2</title>
</head>
<body>
<a href="1.html">Page1</a>
<script src="angus.js"></script>
<form name="frm">
<input type="button" value="Setblue" onclick="setcolour('blue');" />
<input type="button" value="Setyellow" onclick="setcolour('yellow');" />
<input type="button" value="getcolour" onclick="alert(getcolour());" />
</form>
</body>
</html>

If I set a colour in one page and navigate to page 2, and THEN access the colour, it returns undefined. ie I it seems that a new instance of g_colour is created on loading a new html page.

I want to be able to access a sort of top-level variable which I can set in page 1 and access in page 2. How can I do that in Javascript?

Angus Comber
  • 9,316
  • 14
  • 59
  • 107

4 Answers4

11

JS variables never have been persistent, but there are two ways around this:

  1. Cookies
  2. Storage

Cookies are supported in all but the most ancient browsers, but they can be very unwieldly and difficult to use. On top of that, your browser sends cookies to the server with every pageload, so if it's only used by JavaScript then it's very inefficient.

Instead, you should probably look at the Storage option.

Saving an item is as simple as localStorage.itemname = value; Reading is as easy as localStorage.itemname, and deleting is as literal as delete localStorage.itemname

These values are saved across pageloads, but not sent to the server.

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
  • i believe `removeItem` is recommended over `delete` as the latter will not work properly with shimmed implementations. [Related](http://stackoverflow.com/questions/6906494/deleting-from-localstorage-should-i-use-delete-or-removeitem). – jbabey Jan 03 '13 at 13:20
  • I thought cookies was the solution, just wanted to check. I will also investigate localstorage - although being able to run on any browser is an obvious advantage of cookies. – Angus Comber Jan 03 '13 at 13:43
4

Use localStorage:

localStorage.setItem('name', 'value');
var something = localStorage.getItem('name');

setItem on your first page, then getItem on your second.

The localStorage persists across pageloads, as opposed to "normal" JavaScript variables.

"Normal" variables are initialized as soon as the JS file is loaded (And runs), but are destroyed when the file unloads, so when the user leaves a page.

You could also use Cookies, but they're a bit of a pain to work with in JS, since they're stored in a string like:

'name=value; name1=value1; name2=value2';
Cerbrus
  • 70,800
  • 18
  • 132
  • 147
2

Each page request will request the script and execute its copy of it, even if the request stops at the client because of the cache, the current page still executes it from scratch. They are working with the same code, yes, but different instances (i.e. you have two copies of that variable in two different contexts).

Grant Thomas
  • 44,454
  • 10
  • 85
  • 129
0

The problem is that your page 1 is loading the JavaScript file and your page 2 is loading it again therefore whatever you have set in a variable on that JS file will be lost when page 2 is loaded since page 2 will initialize again the JS file. If you want you can use cookie to store the value or if it simple to you combine page 1 and page 2 but put them in a different div and show/hide the div according to your logic.

Jobert Enamno
  • 4,403
  • 8
  • 41
  • 63