Detect Click into Iframe using JavaScript

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

    This definitely works if the iframe is from the same domain as your parent site. I have not tested it for cross-domain sites.

    $(window.frames['YouriFrameId']).click(function(event){  /* do something here  */ });
    $(window.frames['YouriFrameId']).mousedown(function(event){ /* do something here */ });
    $(window.frames['YouriFrameId']).mouseup(function(event){ /* do something here */ });
    

    Without jQuery you could try something like this, but again I have not tried this.

    window.frames['YouriFrameId'].onmousedown = function() { do something here }
    

    You can even filter your results:

    $(window.frames['YouriFrameId']).mousedown(function(event){   
      var eventId = $(event.target).attr('id');      
      if (eventId == 'the-id-you-want') {
       //  do something
      }
    });
    

提交回复
热议问题