How to push oath token to LocalStorage or LocalSes

2019-08-31 14:55发布

问题:

This references this issue: Javascript SDK connect() function not working in chrome

I asked for more information on how to resolve with localstorage and was asked to create a new topic.

The answer was "A workaround is instead of using window.opener, push the oauth token into LocalStorage or SessionStorage and have the opener window listen to the Storage event."

but i have no idea how to do that. It seems really simple, but i don't know where to start. I couldn't find an relevant examples.

thanks for your help!

回答1:

You can attach an event listener to the "storage" event which is fired by the window.

window.addEventListener("storage", myHandler, false);

The handler is passed an event object which includes the key which changed.

function myHandler(event) {
   if (event.key === 'the_oauth_token') {
       // do something with it
   }
}

Here's a demo: http://html5demos.com/storage-events