Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript sessionStorage Across tabs?

I am using sessionStorage to ensure an action only happens once per session. However, if a user opens a new window - the sessionStorage is not carried over to the new window, and the action is executed again. See my code below:

if sessionStorage.alreadyTracked != "1"
  sessionStorage.alreadyTracked = "1"
  ...

Is there an easy way to do this that applies to all tabs? Maybe I need to use a cookie?

like image 353
malexanders Avatar asked Apr 03 '17 18:04

malexanders


2 Answers

As far as html5 storage goes i would go for localStorage in your case.

See below for the differences.

Session Storage:

  • Values persist only as long as the window or tab in which they
    stored.
  • Values are only visible within the window or tab that created them.

Local Storage:

  • Values persist window and browser lifetimes.
  • Values are shared across every window or tab running at the same origin.

Read more here

like image 92
WilomGfx Avatar answered Sep 23 '22 19:09

WilomGfx


You probably want to switch to localStorage as sessionStorage is bound to individual tabs and localStorage is not.

like image 34
Rob M. Avatar answered Sep 19 '22 19:09

Rob M.