How to push oath token to LocalStorage or LocalSession and listen to the Storage Event? (SoundCloud Php/JS bug workaround)

限于喜欢 提交于 2019-12-12 00:12:51

问题


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



来源:https://stackoverflow.com/questions/10842231/how-to-push-oath-token-to-localstorage-or-localsession-and-listen-to-the-storage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!