Detect Click into Iframe using JavaScript

前端 未结 21 2297
轻奢々
轻奢々 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:32

    see http://jsfiddle.net/Lcy797h2/ for my long winded solution that doesn't work reliably in IE

            $(window).on('blur',function(e) {    
                if($(this).data('mouseIn') != 'yes')return;
                $('iframe').filter(function(){
                    return $(this).data('mouseIn') == 'yes';
                }).trigger('iframeclick');    
            });
    
            $(window).mouseenter(function(){
                $(this).data('mouseIn', 'yes');
            }).mouseleave(function(){
                $(this).data('mouseIn', 'no');
            });
    
            $('iframe').mouseenter(function(){
                $(this).data('mouseIn', 'yes');
                $(window).data('mouseIn', 'yes');
            }).mouseleave(function(){
                $(this).data('mouseIn', null);
            });
    
            $('iframe').on('iframeclick', function(){
                console.log('Clicked inside iframe');
                $('#result').text('Clicked inside iframe'); 
            });
            $(window).on('click', function(){
                console.log('Clicked inside window');
                $('#result').text('Clicked inside window'); 
            }).blur(function(){
                console.log('window blur');
            });
    
            $('').appendTo(document.body).blur(function(){
                    $(window).trigger('blur');
                }).focus();
    

提交回复
热议问题