繁体   English   中英

侦听iframe事件-打开网址

[英]Listen for iframe event - on open a url

我的移动网站中嵌入了一个iframe,该iframe的内容包含带有标签的图像。 我想听用户单击iframe中的图像并重定向到新页面的事件。

例如:

<!-- This is the content of the iframe -->
<!-- The hosting location is http://www.example.com/iframe.html -->
<a href="http://www.google.com" target="_blank">
<img src="http://www.example.com/image01.jpg">
</a>

接着

<!-- This is the content of the mobile site -->
<html>
<meta name="viewport" content="user-scalable=no, width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"/>
<iframe src="http://www.example.com/iframe.html"></iframe>
</html>

因此,当用户单击iframe中的图片时,他将被重定向到http://www.example.com ,我想收听此事件,该事件可以动态运行一些javascript函数。

有什么建议么? 谢谢。

2014年12月9日的评论:

必须注意的一件事是,将移动站点的内容动态传递到具有不同域的多个移动站点中。 因此window.parent无法使用,因为iframe内容文件和html文件不在同一个域中。

我还检查了方法postMessage(data,targetDomain)。 但是,它只能在知道iframe内容文件和html文件的域的情况下工作。 (就我而言,html文件的域是未知的,因为它是在具有不同域的多个移动网站上动态传递的)。

任何解决方案?

您可以使用window.parent

在您的iframe页面中:

$('a').on("click", function(e) {
  window.parent.doStuff();
});

在您的主页中:

function doStuff(){ ... }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM