Last active
April 10, 2019 06:44
-
-
Save xixilive/b46b025a62eb257ec5da798ccf2491c3 to your computer and use it in GitHub Desktop.
use storage event in iframe
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function createStorageMessageFrame(){ | |
const listener ='function(e){window.top.postMessage({key: e.key, oldValue: e.oldValue,newValue: e.newValue}, \''+ window.location.origin +'\')}' | |
const frame = document.createElement('iframe') | |
frame.style.width = 0 | |
frame.style.height = 0 | |
frame.style.border = 'none' | |
frame.style.overflow = 'hidden' | |
frame.style.visibility = 'hidden' | |
document.body.appendChild(frame) | |
frame.contentWindow.document.open() | |
frame.contentWindow.document.write("<script>window.addEventListener('storage', "+ listener +")<\/script>") | |
frame.contentWindow.document.close() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment