Invoking JavaScript code in an iframe from the parent page

后端 未结 17 1833
栀梦
栀梦 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:19

    Continuing with JoelAnair's answer:

    For more robustness, use as follows:

    var el = document.getElementById('targetFrame');
    
    if(el.contentWindow)
    {
       el.contentWindow.targetFunction();
    }
    else if(el.contentDocument)
    {
       el.contentDocument.targetFunction();
    }
    

    Workd like charm :)

提交回复
热议问题