1

I have some JavaScript that will look to see if a var is set, if it is set then do something if not don't.

if (reset === 'reset') {
    gallery.unformat(container);
}

reset is only set once the page has loaded. So this script will only execute after the user reloads the page.

If i open a new tab in firefox reset isn't set. If i open a new tab in chrome reset is set.

So for my case chrome handles it correctly and only on the first going to the site does this var get set and thus everything works correctly.

I want to know is do variables propagate across tabs and if so which browsers do what?

infused
  • 24,000
  • 13
  • 68
  • 78
Jamie Hutber
  • 26,790
  • 46
  • 179
  • 291

2 Answers2

6

Variables should not persist across new tabs. I can't reproduce the behaviour you see in Chrome.

You should looking at using cookies, or if you want to be HTML5, look at localStorage.

Both of these are domain specific, rather than per-tab.

E.g. in localStorage, you could go for;

// Check that localStorage is supported in the current browser, and then try
// to retrieve the item.
if ('localStorage' in window && localStorage.getItem('reset') === 'reset') {
    gallery.unformat(container);
}

You'd then be able to set reset later via;

localStorage.setItem('reset', 'reset');
Matt
  • 74,352
  • 26
  • 153
  • 180
  • local storage is an idea for sure. So thanks for the suggestion :) but i'm more concerned as to why chrome is doing this and firefox is not. I will look into this more closely as you're not experiencing it. Maybe i've misunderstood the code written. – Jamie Hutber Jul 19 '12 at 09:49
  • @JamieHutber Yes, you have logic errors in your code for sure. You should post how you are even handling something like "variable is only set if page was reloaded" because that sounds very fragile. – Esailija Jul 19 '12 at 09:50
  • ye, its a contract i started yesterday. I just noticed the bug occuring in chrome and not in Firefox and deduced this problem :) may update the question, but i'll look myself first – Jamie Hutber Jul 19 '12 at 09:54
4

No, variable values should not propagate between tabs - each tab should have its own global namespace, there would be all kinds of security issues if one tab could affect the JavaScript in another.

codebox
  • 19,927
  • 9
  • 63
  • 81