Detect Click into Iframe using JavaScript

前端 未结 21 2287
轻奢々
轻奢々 2020-11-22 03:06

I understand that it is not possible to tell what the user is doing inside an iframe if it is cross domain. What I would like to do is track if the user clicke

相关标签:
21条回答
  • 2020-11-22 03:11

    We can catch all the clicks. The idea is to reset focus on an element outside the iFrame after each click:

        <input type="text" style="position:fixed;top:-1000px;left:-1000px">
        <div id="message"></div>
        <iframe id="iframe" src="//example.com"></iframe>
        <script>
            focus();
            addEventListener('blur', function() {
                if(document.activeElement = document.getElementById('iframe')) {
                    message.innerHTML += 'Clicked';
                    setTimeout(function () {
                        document.querySelector("input").focus();
                        message.innerHTML += ' - Reset focus,';
                    }, 1000);
                }  
            });
        </script>
    

    JSFiddle

    0 讨论(0)
  • 2020-11-22 03:12

    You can achieve this by using the blur event on window element.

    Here is a jQuery plugin for tracking click on iframes (it will fire a custom callback function when an iframe is clicked) : https://github.com/finalclap/iframeTracker-jquery

    Use it like this :

    jQuery(document).ready(function($){
        $('.iframe_wrap iframe').iframeTracker({
            blurCallback: function(){
                // Do something when iframe is clicked (like firing an XHR request)
            }
        });
    });
    
    0 讨论(0)
  • 2020-11-22 03:16

    This is small solution that works in all browsers even IE8:

    var monitor = setInterval(function(){
        var elem = document.activeElement;
        if(elem && elem.tagName == 'IFRAME'){
            clearInterval(monitor);
            alert('clicked!');
        }
    }, 100);
    

    You can test it here: http://jsfiddle.net/oqjgzsm0/

    0 讨论(0)
  • 2020-11-22 03:17

    Just found this solution... I tried it, I loved it..

    Works for cross domain iframes for desktop and mobile!

    Don't know if it is foolproof yet

    window.addEventListener('blur',function(){
          if(document.activeElement.id == 'CrossDomainiframeId'){
            //do something :-)
          }
    });
    

    Happy coding

    0 讨论(0)
  • 2020-11-22 03:20

    Is something like this possible?

    No. All you can do is detect the mouse going into the iframe, and potentially (though not reliably) when it comes back out (ie. trying to work out the difference between the pointer passing over the ad on its way somewhere else versus lingering on the ad).

    I imagine a scenario where there is an invisible div on top of the iframe and the the div will just then pass the click event to the iframe.

    Nope, there is no way to fake a click event.

    By catching the mousedown you'd prevent the original click from getting to the iframe. If you could determine when the mouse button was about to be pressed you could try to get the invisible div out of the way so that the click would go through... but there is also no event that fires just before a mousedown.

    You could try to guess, for example by looking to see if the pointer has come to rest, guessing a click might be about to come. But it's totally unreliable, and if you fail you've just lost yourself a click-through.

    0 讨论(0)
  • 2020-11-22 03:20

    http://jsfiddle.net/QcAee/406/

    Just make a invisible layer over the iframe that go back when click and go up when mouseleave event will be fired !!
    Need jQuery

    this solution don't propagate first click inside iframe!

    $("#invisible_layer").on("click",function(){
    		alert("click");
    		$("#invisible_layer").css("z-index",-11);
    
    });
    $("iframe").on("mouseleave",function(){
    		$("#invisible_layer").css("z-index",11);
    });
    iframe {
        width: 500px;
        height: 300px;
    }
    #invisible_layer{
      position: absolute;
      background-color:trasparent;
      width: 500px;
      height:300px;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div id="message"></div>
    <div id="invisible_layer">
    
    </div>
    <iframe id="iframe" src="//example.com"></iframe>

    0 讨论(0)
提交回复
热议问题