开发者

jQuery Click Tracking not working on iFrame

开发者 https://www.devze.com 2023-03-22 06:29 出处:网络
First, here is my code: <script type=\"text/javascript\"> $(\'#Advertisement-1\').click(function () {

First, here is my code:

<script type="text/javascript">
    $('#Advertisement-1').click(function () { 
        alert("Ad Clicked!"); 
    });
</script>

<div id="Advertisement-1">
    <!-- PBBG Ads Zone Code Begin -->
    <center>
        <iframe src='http://www.pbbgads.com/ad.php?z=429&bg=000000' width='468' height='67' marginwidth='0' marginheight='0' hspace='0' vspace='0' frameborder='0' scrolling='no'&g开发者_开发技巧t;</iframe>
    </center>
    <!-- PBBG Ads Zone Code End -->
</div>

Now, my issue is when I click the ad, it doesn't send an alert. It just opens the ad in a new window. Any ideas how I can get the click event to work?


click event doesn't works on iframes, because the event does not bubble up through the <iframe> tag's ancestors in the DOM.

Instead you can use the blur event to detect when your parent page is losing focus. This jQuery plugin is designed to track clicks on iframes : https://github.com/finalclap/iframeTracker-jquery

It's very easy to use, simply select your iframe with a selector, and supply a callback function (will be fired when the iframe is clicked) :

jQuery(document).ready(function($){
    $('.iframe_wrap iframe').iframeTracker({
        blurCallback: function(){
            // Do something when iframe is clicked (like firing an XHR request)
        }
    });
});


The code to alert is in the parent window and the ad I suppose is inside the iframe so it is not possible unless the parent page and iframe are in same domain.


You can't unless you're detecting the click from http://www.pbbgads.com/

0

精彩评论

暂无评论...
验证码 换一张
取 消