Warm tip: This article is reproduced from stackoverflow.com, please click
html iframe javascript redirect

Trigger redirection to another page if specific page is loaded within an iframe

发布于 2020-04-05 00:23:24

I am using Limesurvey to create a customer survey. As I want to embed the survey at my company's homepage, I am using an iframe to make that happen. This ensures that I have my official header and footer available and I don't have to rebuild that elements in Limesurvey.

Limesurvey offers a possibility to redirect to another page after somebody has completed the survey (e.g. a thank you page). As I am embedding Limesurvey, I cannot redirect to my offical thank you site from my company's homepage as I would have double header and double footer.

What I have thought of, is to redirect to a dummy site. From the parent site, I want to check for that event and trigger my redirection to my thank you site.

I have no idea how to do that in Javascript. How do I define an event listener to check if a specific site has been loaded within the iframe and then fire my redirection in the parent.

Thanks a lot!

Questioner
Sven
Viewed
57
Expired Data 2020-01-31 23:01

You can inspect the URL in the IFrame using <element>.documentWindow.location.href if it is on the same domain:

e.g if your iframe had id 'frame'

document.getElementById('frame').documentWindow.location.href

However, You can't inspect the URL inside an IFrame if the IFrame is on a different domain, this is due to a security mechanism in modern browsers.

If you are not in the same page it will raise an exception:

Error: Permission denied to get property Location.href

See this SO answer for more detail also as noted here the property is named differently in chrome: documentWindow is called contentDocument in Chrome