Javascript 如何调用子<frame>中指定的javascript函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5946656/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to call javascript function specified in a child <frame>
提问by LoomyBear
I'm working on a ancient project which is based on and tags.
我正在研究一个基于和标签的古老项目。
Here's the common HTML structure of the parent page (index.html):
下面是父页面 (index.html) 的常见 HTML 结构:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
</head>
<frameset rows="75px,*" frameborder="0">
<frame name="frame1" src="frames/frame1.html">
<frameset class="child_frameset">
<frame name="frame3" src="frames/frame3.html">
</frameset>
</frameset>
</html>
Here how I specify the javascript function on the child frame (frame3.html):
这里我如何在子框架(frame3.html)上指定javascript函数:
<!DOCTYPE html>
<html lang="en">
<head>
<script type="text/javascript">
function helloWorld(){
alert('hello world!');
}
</script>
</head>
<body>
<h1>I'm frame 3</h1>
</body>
</html>
My task is to call helloWorld()function. How do I do that?
Thanks in advance!
我的任务是调用helloWorld()函数。我怎么做?
提前致谢!
回答by Jayantha Lal Sirisena
You can do it like this:
你可以这样做:
document.getElementById('frame3').contentWindow.helloWorld();
回答by ranjeetcao
Check for contentWindow, contentDocument and get the frame object and perform call.
检查 contentWindow、contentDocument 并获取框架对象并执行调用。
var siblingFrame;
if (parent.document.getElementById('siblingFrameId').contentWindow){
siblingFrame = parent.document.getElementById('siblingFrameId').contentWindow;
} else if (parent.document.getElementById('siblingFrameId').contentDocument){
siblingFrame = parent.document.getElementById('siblingFrameId').contentDocument;
} else if (parent.document.getElementById('siblingFrameId')) {
siblingFrame = parent.document.getElementById('siblingFrameId');
}
siblingFrame.helloWorld();
回答by allsyed
You can call method from childframe
您可以从子框架调用方法
window.frames[n].frames[m].methodName()
where n , m are integers starting from 0.If you have document only one frame remove the frames[m] part and get the job done.
其中 n , m 是从 0 开始的整数。如果您只有一帧文档,请删除 frame[m] 部分并完成工作。
Ya one more thing , do trial an error and find out exact frame numbers and method. Hope this helps
雅还有一件事,尝试错误并找出确切的帧数和方法。希望这可以帮助