2

Example:

  1. In the main page cliked on a button (NEW), the page then will using Javascript to open a new page in a new window by calling redirectPage().

  2. In the main page clicked on a button (EXIT), then page then will call confirmExit(), then closeChildWindows() to closed all popup new window before redirect to another new page.

  3. However, the JS variable (childWindowHandles) will be always reset if I refresh the main page, and this cause the page unable to close all other popup window before relocated while EXIT button being clicked

Does anyone know how can I solve this problem? By able to keep the JS variable (childWindowHandles) even the main page being refresh?

var childWindowHandles = new Array();

function redirectPage(url)
{
    childWindowHandles[childWindowHandles.length] = window.open(url)
}

function confirmExit(url)
{
    closeChildWindows()
    window.location=url
}

function closeChildWindows() 
{
    for (var loop=0; loop<childWindowHandles.length; loop++) 
    {
        if (!childWindowHandles[loop].closed)
        {
                childWindowHandles[loop].close();
        }
    }
}
karim79
  • 339,989
  • 67
  • 413
  • 406
Jin Yong
  • 42,698
  • 72
  • 141
  • 187
  • possible duplicate of [Global Variable usage on document.ready reload](http://stackoverflow.com/questions/29986657/global-variable-usage-on-document-ready-reload) – Stephan Muller Jun 05 '15 at 09:39

6 Answers6

10

You can use cookies to persist values...

Edit: You might find useful a simple object that I use:

Usage:

// Store a key/value for 1 day:
cookieManager.set('name', 'a value', 1);

// Retrieve a value associated to a key:
var value = cookieManager.get('name');

// Remove a key/value:
cookieManager.remove('name');

Implementation:

var cookieManager = { 
  set: function (name, value, expireDays) { 
    var expireDate = new Date(); 
    expireDate.setDate(expireDate.getDate() + expireDays); 

    document.cookie = name + "=" + escape(value) + 
      ((!expireDays) ? "" : ";expires="+expireDate.toGMTString()); 
  }, 

  get: function (key) { 
    var start,end; 

    if (document.cookie.length > 0) { 
      start = document.cookie.indexOf(key + "="); 

      if (start != -1) { 
        start = start + key.length + 1; 
        end = document.cookie.indexOf(";",start); 

        if (end == -1) { 
          end = document.cookie.length; 
        }
        return unescape(document.cookie.substring(start,end)); 
      }
    }
    return ""; 
  },

  remove: function (key) {
    this.set(key, '', -1);
  }
}
Christian C. Salvadó
  • 807,428
  • 183
  • 922
  • 838
3

You can use cookies or window.name:) window.name to store session variables

Eldar Djafarov
  • 23,327
  • 2
  • 33
  • 27
  • +1: Simple values > `window.name` is a winner. Although there's a catch that (not in latest browser versions thankfully) this value preserves over other domains until it's being reset by any... We have to be careful not to store any volatile data in it. – Robert Koritnik Sep 26 '13 at 07:04
2

Per this post here on SO, Firefox 3.5, Safari 4, and IE8 support HTML5 Storage.

Community
  • 1
  • 1
Sam Harwell
  • 97,721
  • 20
  • 209
  • 280
1

Or use PersistJS which simplifies your access to whichever back-end storage mechanisms are available. (But cookie-less)

davewasthere
  • 2,988
  • 2
  • 24
  • 25
1

Use window.name

Positives:

  • it will live for the time of browser session - user closes window and it's gone
  • it won't put additional traffic on the wire like cookies do
  • it works even when cookies are disabled
  • at least 2MB space (Opera's limit is this low, other's have 32/64MB)

I also suggest you use javascript object for storing various values and serialize it using JSON and put that string into window.name.

Just make sure you don't persist any vulnerable data inside... For security reasons.

Robert Koritnik
  • 103,639
  • 52
  • 277
  • 404
  • Are you sure that is not reset after page refresh? – Gary Sep 25 '13 at 01:27
  • 1
    @Gary: It's not reset. Check [this demo](http://jsbin.com/oWUgUqA/1). When it runs the first time, it sets `window.name` and then it refreshes the page every 2 seconds and displays what's stored in `window.name` that is preserved after refresh. – Robert Koritnik Sep 26 '13 at 06:57
0

You can use sessionStorage.

Check this out:

html5_webstorage

Hasan Fathi
  • 5,610
  • 4
  • 42
  • 60
Mert Mertce
  • 1,614
  • 3
  • 21
  • 32