当iframe更改/加载时显示窗口

时间:2011-04-24 07:50:50

标签: javascript jquery iframe

我正在使用此脚本,当用户点击链接时会显示一些警告:

 <script>
var disclaimer="This link is not our responsibility - click ok to continue"
iframe.onload=function() {
  var links = document.links;
  for (var i=0, n=links.length;i<n;i++) {
    links[i].onclick=function() { return confirm(disclaimer);}
  }
}
</script>

我在网站上有一个iframe,我想在用户点击iframe上的链接时触发相同的窗口。有没有办法用普通的javascript做到这一点?

非常感谢

2 个答案:

答案 0 :(得分:0)

假设protocol, domain and port matches your page,您可以使用iframe属性访问contentDocument的文档(这是标准,但您可能需要使用contentWindow.document

var iframe = document.getElementsByTagName('iframe')[0],
    iframeDocument;

if ('contentDocument' in iframe) {
    iframeDocument = contentDocument;
} else {
    iframeDocument = iframe.contentWindow.document;
}

答案 1 :(得分:0)

您可以使用 parent 关键字来调用当前窗口而不是iframe窗口。

例如,(在iframe中)

parent.confirm(disclaimer);
相关问题