Javascript 如何在javascript中调用窗口子函数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13953321/
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 can i call a window child function in javascript?
提问by Jonathan
I am calling the parent function like window.parent.functionname();from the child page. How can i call the window.child.function()from the parent page to child page.
我正在像window.parent.functionname();从子页面一样调用父函数。如何window.child.function()从父页面调用子页面。
Any help is appreciated.
任何帮助表示赞赏。
回答by Bruno
Give your iFrame an id and try
给你的 iFrame 一个 id 并尝试
document.getElementById("iFrameId").contentWindow.functionname()
This works even when you have multiple iFrames in the same page irrespective of their order.
即使您在同一页面中有多个 iFrame,而不管它们的顺序如何,这也有效。
回答by algorhythm
Do you have an iframe?
你有 iframe 吗?
Do something like that:
做这样的事情:
window.frames[0].contentDocument.functionname();
回答by pala?н
Parent page
父页面
var windowRef = null;
function openChildWindow() {
if ((windowRef != null) && (windowRef.closed == false)) {
if (windowRef.closed == false) windowRef.close();
windowRef = null;
}
var windowUrl = 'ChildPage.aspx';
var windowId = 'NewWindow_' + new Date().getTime();
var windowFeatures = 'channelmode=no,directories=no,fullscreen=no,' + 'location=no,dependent=yes,menubar=no,resizable=no,scrollbars=yes,' + 'status=no,toolbar=no,titlebar=no,' + 'left=0,top=0,width=400px,height=200px';
windowRef = window.open(windowUrl, windowId, windowFeatures);
windowRef.focus();
// Need to call on a delay to allow
// the child window to fully load...
window.setTimeout(callChildWindowFunction(), 1000);
}
function callChildWindowFunction() {
if ((windowRef != null) && (windowRef.closed == false)) windowRef.childWindowFunction();
}?
Child Page
子页面
function childWindowFunction() {
alert('Hello from childWindowFunction()');
}?
回答by Tito100
var win = null;
function openAndCall(id){
if (win!=null && win.closed==false){
win.close();
}
win = window.open("/somePage");
win.onload = function(){
win.callSome(id);
};
}

