How to trigger a function when iframe src change?

13,320
var iframe = document.getElementById("iframe");

iframe.addEventListener("DOMAttrModified", function(event) {
    if (event.attrName == "src") {
       // The "src" attribute changed
    }
});

However this will work only in modern browsers

Share:
13,320

Related videos on Youtube

rkaartikeyan
Author by

rkaartikeyan

I am a Website Developer using Both PHP and ASP.

Updated on September 17, 2022

Comments

  • rkaartikeyan
    rkaartikeyan about 1 year

    i have iframe with src like http://remote-domain.com/, and when ever the src is get changed i need to trigger a function like iframeSrcChanged()

    <iframe id="iframe" src="http://remote-domain.com/" frameborder="0" width="100%" height="100%" ></iframe>
    

    and when user click about-us inside iframe, the iframe src get changed into

    <iframe id="iframe" src="http://remote-domain.com/about-us.html" frameborder="0" width="100%" height="100%" ></iframe>
    

    so this time i need to trigger iframeSrcChanged() function.

    Please help me to trigger out this issue.

  • rkaartikeyan
    rkaartikeyan about 10 years
    Thanks for the quick reply. its not working for me :(
  • Tsanyo Tsanev
    Tsanyo Tsanev about 10 years
    Even though this would work, using it is highly discouridged. The event won't fire in many browsers, see developer.mozilla.org/en-US/docs/Web/Guide/API/DOM/Events/… and help.dottoro.com/ljdchxcl.php
  • Tsanyo Tsanev
    Tsanyo Tsanev about 10 years
    jsfiddle.net/HwY9Q/4 - the event will trigger, but you can get only the src property if the domain name of the iframe is different than your site's domain (the browser won't let you access the contentWindow, for security reasons).