我只是想出了如何在跨域iframe上检测点击事件,但它仅适用于台式机,以下代码在检测iframe内的点击事件时有效,但是,我还需要它在移动设备上运行,我尝试使用touchstart
和touchend
事件向此脚本添加移动支持,但不起作用。
//Google ADs track conversion
$( document ).ready(function() {
var iframeMouseOver = false;
var iframeTouched = false;
$("#wh-widget-send-button")
.off("mouseover.iframe").on("mouseover.iframe",function() {
iframeMouseOver = true;
})
.off("mouseout.iframe").on("mouseout.iframe",function() {
iframeMouseOver = false;
});
//Add mobile support to this script
$("#wh-widget-send-button")
.off("touchstart").on("touchstart",function() {
iframeTouched = true;
})
.off("touchend").on("touchend",function() {
iframeTouched = false;
});
$(window).off("blur.iframe").on("blur.iframe",function() {
if(iframeMouseOver || iframeTouched){
console.log("Iframe Clicked");
gtag_report_conversion();
}
});
});
更新
所请求的HTML只是div中的一个简单iframe,还清除了上面的代码以专注于重要部分:
<div id="wh-widget-send-button">
<iframe src="http://anyexternaldomain.com"></iframe>
</div>