2013-10-21 54 views
3

我有iframe與SRC像http://remote-domain.com/,當有史以來src爲得到改變,我需要引起類似​​如何在iframe src更改時觸發某個函數?

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

,當用戶點擊有關,我們內部的iframe的功能,在IFRAME SRC會改成

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

所以這次我需要觸發​​函數。

請幫我解決這個問題。

+0

請參閱 http://stackoverflow.com/questions/2429045/iframe-src-change-event-detection –

回答

2
var iframe = document.getElementById("iframe"); 

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

然而,這將在現代瀏覽器只工作

+0

感謝您的快速回復。它不爲我工作:( – rkaartikeyan

+0

即使這會工作,使用它是非常disouridged。事件不會在許多瀏覽器中觸發,請參閱https://developer.mozilla.org/en-US/docs/Web/Guide/API/DOM/Events/Mutation_events和http://help.dottoro.com/ljdchxcl.php –

+0

檢查此JSFiddle http://jsfiddle.net/HwY9Q/3/ – rkaartikeyan