3

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
  • 1,977
  • 9
  • 29
  • 57

1 Answers1

2
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

Ricky Stam
  • 2,116
  • 21
  • 25
  • 1
    Even though this would work, using it is highly discouridged. The event won't fire in many browsers, see https://developer.mozilla.org/en-US/docs/Web/Guide/API/DOM/Events/Mutation_events and http://help.dottoro.com/ljdchxcl.php – Tsanyo Tsanev Oct 21 '13 at 15:47
  • 1
    http://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). – Tsanyo Tsanev Oct 21 '13 at 16:15