【问题标题】:Click event not working on google ads iframe点击事件不适用于谷歌广告 iframe
【发布时间】:2013-08-02 06:29:43
【问题描述】:

我正在尝试制作一个脚本来检测对谷歌广告的点击。除了单击、焦点、鼠标按下之外,所有事件都像 onmouseover、out 等一样工作。当我点击谷歌广告时,它会打开它的广告链接,但不会在点击事件上运行我的 jquery 脚本。我也尝试过 preventdefault 和其他类似的功能。 我想检测对谷歌广告的点击,并在点击时将 iframe 的宽度和高度解析为 php 文件。

$(window).load(function(){
    $('iframe').bind('mouseover', function(){
        var iframeID = $(this).attr('id');
        $(this).contents().find('html').unbind();
        $(this).contents().find('html').bind('click', function(){
            alert(iframeID);
        });
    });
});

这是在鼠标悬停而非点击事件时运行的代码。

【问题讨论】:

    标签: php javascript jquery google-ad-manager


    【解决方案1】:

    您需要在 iframe 的内容上绑定点击事件,直到内容完全加载到 iframe 中。尝试将代码封装在 load 事件中。

      $('iframe').load(function() {
           var iframeID = $(this).attr('id');
           $(this).contents().find('html').unbind();
           $(this).contents().find('html').bind('click', function(){
               alert(iframeID);
           });
      });
    

    【讨论】:

    • 不安全,因此不适用于许多浏览器。谷歌广告来自不同的域,所以不可能。
    • 不适用于谷歌广告。 Google 广告会在点击时打开其广告链接。
    【解决方案2】:

    当内容来自另一个域时,无法捕获 iFrame 的点击事件。

    您可以做的是检测鼠标进入和离开 iframe。

    这里是例子

    <div class="item">
    <iframe src="http://www.google.com" width="612" height="344" frameborder="0"></iframe>
    </div>
    
    <script type="text/javascript">
    $("div.item iframe")
    .mouseover(function(){
        alert("mouse over");
    })
    .mouseout(function(){
        alert("mouse out");
    });
    </script>
    

    【讨论】:

    • 我正在制作一个 google 分析类型脚本,供您参考 statcounter 检测对 google 广告的点击并在其退出链接活动部分提供信息。我想做和 statcounter 一样的事情
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-03
    • 1970-01-01
    • 1970-01-01
    • 2021-01-11
    • 2011-10-16
    相关资源
    最近更新 更多