Detect click event inside iframe

前端 未结 9 2143
刺人心
刺人心 2020-11-28 05:40

I\'m writing a plugin for TinyMCE and have a problem with detecting click events inside an iframe.

From my search I\'ve come up with this:

Loading iframe:

相关标签:
9条回答
  • 2020-11-28 06:14

    In my case, I was trying to fire a custom event from the parent document, and receive it in the child iframe, so I had to do the following:

    var event = new CustomEvent('marker-metrics', {
        detail: // extra payload data here
    });
    var iframe = document.getElementsByTagName('iframe');
    iframe[0].contentDocument.dispatchEvent(event)
    

    and in the iframe document:

    document.addEventListener('marker-metrics', (e) => {
      console.log('@@@@@', e.detail);
    });
    
    0 讨论(0)
  • 2020-11-28 06:16
    $("#iframe-id").load( function() {
        $("#iframe-id").contents().on("click", ".child-node", function() {
            //do something
        });
    });
    
    0 讨论(0)
  • 2020-11-28 06:18

    I'm not sure, but you may be able to just use

    $("#filecontainer #choose_pic").click(function() {
        // do something here
    });
    

    Either that or you could just add a <script> tag into the iframe (if you have access to the code inside), and then use window.parent.DoSomething() in the frame, with the code

    function DoSomething() {
        // do something here
    }
    

    in the parent. If none of those work, try window.postMessage. Here is some info on that.

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