Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
239 views
in Technique[技术] by (71.8m points)

javascript - 从父页面在iframe中调用JavaScript代码(Invoking JavaScript code in an iframe from the parent page)

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

(基本上,我将iframe嵌入到页面中,并且iframe具有一些我需要从父页面调用的JavaScript例程。)

Now the opposite is quite simple as you only need to call parent.functionName() , but unfortunately, I need exactly the opposite of that.

(现在相反非常简单,因为您只需要调用parent.functionName() ,但是不幸的是,我需要恰好相反。)

Please note that my problem is not changing the source URL of the iframe , but invoking a function defined in the iframe .

(请注意,我的问题不是更改iframe的源URL ,而是调用iframe定义的函数。)

  ask by Tamas Czinege translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

(假设您的iFrame的ID为“ targetFrame”,而您要调用的函数为targetFunction() :)

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

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

(您也可以使用window.frames而不是document.getElementById来访问框架。)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...