Invoking JavaScript code in an iframe from the parent page

后端 未结 17 1788
栀梦
栀梦 2020-11-21 07:00

Basically, I have an iframe embedded in a page and the iframe has some JavaScript routines I need to invoke from the parent page.

Now the o

17条回答
  •  借酒劲吻你
    2020-11-21 07:13

    In the IFRAME, make your function public to the window object:

    window.myFunction = function(args) {
       doStuff();
    }
    

    For access from the parent page, use this:

    var iframe = document.getElementById("iframeId");
    iframe.contentWindow.myFunction(args);
    

提交回复
热议问题