"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > How to Detect iFrame Source Changes from the Parent Page?

How to Detect iFrame Source Changes from the Parent Page?

Published on 2024-11-08
Browse:609

How to Detect iFrame Source Changes from the Parent Page?

How to Detect iFrame Source Changes on Parent Page

When you lack control over the content within an iFrame, it can be challenging to monitor changes to its source URL. However, there are techniques you can employ to detect these events via the parent page.

One option is to utilize the onLoad event. By adding an onLoad attribute to your iFrame element, you can specify a function to execute whenever the iFrame's source changes. For example:

This setup will trigger the alert every time the location within the iFrame is modified. It functions in most modern browsers, but may not work in older browsers (e.g., IE5, early Opera).

Alternatively, if the iFrame is displaying a page within the same domain as the parent, you can access the iFrame's location through contentWindow.location. This allows you to monitor changes directly:

These approaches provide reliable ways to detect iFrame source changes, eliminating the need for hacky solutions like interval tests.

Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3