Detect Click into Iframe using JavaScript

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

    You can do this to bubble events to parent document:

    $('iframe').load(function() {
        var eventlist = 'click dblclick \
                        blur focus focusin focusout \
                        keydown keypress keyup \
                        mousedown mouseenter mouseleave mousemove mouseover mouseout mouseup mousemove \
                        touchstart touchend touchcancel touchleave touchmove';
    
        var iframe = $('iframe').contents().find('html');
    
        // Bubble events to parent
        iframe.on(eventlist, function(event) {
            $('html').trigger(event);
        });
    });
    

    Just extend the eventlist for more events.

提交回复
热议问题