将点击应用于Iframe [英] Apply onClick to Iframe

查看:93
本文介绍了将点击应用于Iframe的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么办法可以用一些其他元素封装一个iframe来模拟onClick?我知道Iframe不支持事件,但我需要跟踪从我的网站上通过iframe离开的点击。

Is there any way to "encapsulate" an Iframe with some other element to simulate onClick? I know the Iframe doesn't support events but I need to track clicks from my website that are leaving thru the Iframe.

谢谢!

推荐答案

如果框架包含来自同一个域的页面(不违反同源策略),您可以直接与其文档交互:

If the frame contains page from the same domain (does not violate same-origin policy), you can interact directly with its document:

<script type="text/javascript">
window.onload = function() {
    var oFrame = document.getElementById("myframe");
    oFrame.contentWindow.document.onclick = function() {
        alert("frame contents clicked");
    };
};
</script>

如果它包含外部页面,那么您的运气不好 - 没有办法做到您想要的明显的安全原因。虽然所有内容都是同一页面的可视部分,但来自不同域的帧必须在脚本方面保持独立。否则,任何页面都可能创建一个隐藏的iframe加载您的webmail并从中窃取您的会话cookie。所有的数据都可以被用户访问,但它不应该被页面作者访问。

If it contains external page then you're out of luck - no way to do what you want for obvious security reasons. Although all the contents is visually parts of the same page, frames coming from different domains must stay separate in terms of scripting. Otherwise any page could e.g. create a hidden iframe loading your webmail and steal your session cookie from it. All the data is accessible to the user, but it should not be accessible to the page author.

这篇关于将点击应用于Iframe的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆