javascript iframe src 更改时如何触发功能?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19498767/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to trigger a function when iframe src change?
提问by rkaartikeyan
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()
我有类似 src 的 iframe http://remote-domain.com/
,当 src 发生变化时,我需要触发一个类似的函数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 中单击 about-us 时,iframe src 将更改为
<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.
所以这次我需要触发iframeSrcChanged()
功能。
Please help me to trigger out this issue.
请帮我触发这个问题。
回答by Ricky Stam
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
然而,这只适用于现代浏览器