Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

carry forward session storage one page to another page in javascript

// transfers sessionStorage from one tab to another
var sessionStorage_transfer = function(event) {
  if(!event) { event = window.event; } // ie suq
  if(!event.newValue) return;          // do nothing if no value to work with
  if (event.key == 'getSessionStorage') {
    // another tab asked for the sessionStorage -> send it
    localStorage.setItem('sessionStorage', JSON.stringify(sessionStorage));
    // the other tab should now have it, so we're done with it.
    localStorage.removeItem('sessionStorage'); // <- could do short timeout as well.
  } else if (event.key == 'sessionStorage' && !sessionStorage.length) {
    // another tab sent data <- get it
    var data = JSON.parse(event.newValue);
    for (var key in data) {
      sessionStorage.setItem(key, data[key]);
    }
  }
};

// listen for changes to localStorage
if(window.addEventListener) {
  window.addEventListener("storage", sessionStorage_transfer, false);
} else {
  window.attachEvent("onstorage", sessionStorage_transfer);
};


// Ask other tabs for session storage (this is ONLY to trigger event)
if (!sessionStorage.length) {
  localStorage.setItem('getSessionStorage', 'foobar');
  localStorage.removeItem('getSessionStorage', 'foobar');
};
Comment

PREVIOUS NEXT
Code Example
Javascript :: Getting data from one axios to another in componentDidMount 
Javascript :: Could not read source map for f@google-cloud/storage/build/src/channel.js: ENOENT: no such file or directory 
Javascript :: expo draw over other apps 
Javascript :: 3.4.2. Two Special Characters¶ 
Javascript :: 4.7.1. The String Operator +¶ 
Javascript :: alpinejs mail input 
Javascript :: morgan nodejs github 
Javascript :: handlebars.registerHelper is not a function 
Javascript :: event pooling in react/event.persist/using async function in event callback 
Javascript :: bootstrap tab with nvd3 graphs in it 
Javascript :: angular observer if not length 
Javascript :: callbacks 
Javascript :: js regex replace unsafe regex 
Javascript :: strict scalar types 
Javascript :: when i send req upload image in node give Error: ENOENT: no such file or directory,ues multer 
Javascript :: scroll down react js typescript 
Javascript :: deez 
Javascript :: jquery to json diff 
Javascript :: serverless unsupported function event 
Javascript :: decode hex to string js 
Javascript :: window.getselection outside 
Javascript :: Timeout - Async callback was not invoked within the 5000 ms timeout specified by jest.setTimeout. 
Javascript :: create serverless hello-world 
Javascript :: select value from select and pass it to useeffect 
Javascript :: Node temp = new Node(6, head, head.getNext()); head.setNext(temp); temp.getNext().setPrev(temp); Node temp1 = tail.getPrev(); tail.setPrev(temp1.getPrev()); temp1.getPrev().setNext(tail); 
Javascript :: call apply mnemonic javascript 
Javascript :: https://social-network.samuraijs.com/article/faq_po_api 
Javascript :: notion value of empte date property 
Javascript :: dot notation vs bracket notation javascript 
Javascript :: Node_connect 
ADD CONTENT
Topic
Content
Source link
Name
6+3 =