将 onClick 应用到 Iframe [英] Apply onClick to Iframe

查看:30
本文介绍了将 onClick 应用到 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 加载您的网络邮件并从中窃取您的会话 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.

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

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