Invoking JavaScript code in an iframe from the parent page

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

    Assume your iFrame's id is "targetFrame" and the function you want to call is targetFunction():

    document.getElementById('targetFrame').contentWindow.targetFunction();
    

    You can also access the frame using window.frames instead of document.getElementById.

    // this option does not work in most of latest versions of chrome and Firefox
    window.frames[0].frameElement.contentWindow.targetFunction(); 
    

提交回复
热议问题