Detect Click into Iframe using JavaScript

前端 未结 21 2705
轻奢々
轻奢々 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条回答
  •  猫巷女王i
    2020-11-22 03:31

    I ran into a situation where I had to track clicks on a social media button pulled in through an iframe. A new window would be opened when the button was clicked. Here was my solution:

    var iframeClick = function () {
        var isOverIframe = false,
        windowLostBlur = function () {
            if (isOverIframe === true) {
                // DO STUFF
                isOverIframe = false;
            }
        };
        jQuery(window).focus();
        jQuery('#iframe').mouseenter(function(){
            isOverIframe = true;
            console.log(isOverIframe);
        });
        jQuery('#iframe').mouseleave(function(){
            isOverIframe = false;
            console.log(isOverIframe);
        });
        jQuery(window).blur(function () {
            windowLostBlur();
        });
    };
    iframeClick();
    

提交回复
热议问题