Assuming I have no control over the content in the iframe, is there any way that I can detect a src change in it via the parent page? Some sort of onload maybe?
My last resort is to do a 1 second interval test if the iframe src is the same as it was before, but doing this hacky solution would suck.
I'm using the jQuery library if it helps.
Other answers proposed the
load
event, but it fires after the new page in the iframe is loaded. You might need to be notified immediately after the URL changes, not after the new page is loaded.Here's a plain JavaScript solution:
This will successfully track the
src
attribute changes, as well as any URL changes made from within the iframe itself.Tested in all modern browsers.
Note: The above snippet would only work if the iframe is with the same origin.
I made a gist with this code as well. You can check my other answer too. It goes a bit in-depth into how this works.