-
-
Save hvvvva/54a3028685136c2d61d2b4a2cb051cf3 to your computer and use it in GitHub Desktop.
iframe URL redirect listener
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 iframeURLChange(iframe, callback) { | |
var unloadHandler = function () { | |
// Timeout needed because the URL changes immediately after | |
// the `unload` event is dispatched. | |
setTimeout(function () { | |
callback(iframe.contentWindow.location.href); | |
}, 0); | |
}; | |
function attachUnload() { | |
// Remove the unloadHandler in case it was already attached. | |
// Otherwise, the change will be dispatched twice. | |
iframe.contentWindow.removeEventListener("unload", unloadHandler); | |
iframe.contentWindow.addEventListener("unload", unloadHandler); | |
} | |
iframe.addEventListener("load", attachUnload); | |
attachUnload(); | |
} | |
iframeURLChange(document.getElementById("mainframe"), function (newURL) { | |
console.log("URL changed:", newURL); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment