Skip to content

Instantly share code, notes, and snippets.

@xixilive
Last active April 10, 2019 06:44
Show Gist options
  • Save xixilive/b46b025a62eb257ec5da798ccf2491c3 to your computer and use it in GitHub Desktop.
Save xixilive/b46b025a62eb257ec5da798ccf2491c3 to your computer and use it in GitHub Desktop.
use storage event in iframe
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