admin管理员组

文章数量:1188224

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.

Any help is appreciated.

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.

Any help is appreciated.

Share Improve this question edited Dec 19, 2012 at 13:25 Bergi 664k159 gold badges1k silver badges1.5k bronze badges asked Dec 19, 2012 at 13:20 JonathanJonathan 1,6699 gold badges35 silver badges55 bronze badges 4
  • I am guessing you are talking about calling a function within an iFrame within the current document. If so please add that to your question. – Bruno Commented Dec 19, 2012 at 13:22
  • 1 No i am not having any iframe – Jonathan Commented Dec 19, 2012 at 13:28
  • if you're using window.parent and it's working, then you have an iframe. – jbabey Commented Dec 19, 2012 at 13:29
  • It will be in the tabstrip. I am using dhtmlx tabstrip to load the child pages. some code: tabbar.setHrefMode("iframes-on-demand"); tabbar.setContentHref("a1", "../../MailContactMain/Index"); Then can i call using a1 id – Jonathan Commented Dec 19, 2012 at 13:30
Add a comment  | 

4 Answers 4

Reset to default 11

Give your iFrame an id and try

document.getElementById("iFrameId").contentWindow.functionname()

This works even when you have multiple iFrames in the same page irrespective of their order.

Do you have an iframe?

Do something like that:

window.frames[0].contentDocument.functionname();

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()');
}​
var win = null;
function openAndCall(id){
    if (win!=null && win.closed==false){
        win.close();
    }
    win = window.open("/somePage");
    win.onload = function(){
        win.callSome(id);
    };
}

本文标签: framesHow can i call a window child function in javascriptStack Overflow