10

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.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
Jonathan
  • 1,659
  • 8
  • 34
  • 54
  • 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 Dec 19 '12 at 13:22
  • 1
    No i am not having any iframe – Jonathan Dec 19 '12 at 13:28
  • if you're using `window.parent` and it's working, then you have an iframe. – jbabey Dec 19 '12 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 Dec 19 '12 at 13:30

4 Answers4

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.

Bruno
  • 5,772
  • 1
  • 26
  • 43
4

Do you have an iframe?

Do something like that:

window.frames[0].contentDocument.functionname();
algorhythm
  • 8,530
  • 3
  • 35
  • 47
  • 1
    I am not having any frames. Then how can i do that – Jonathan Dec 19 '12 at 13:30
  • @Naidu And where or what is your child? When you make window.parent in which context are you? Give me more code?! – algorhythm Dec 19 '12 at 13:31
  • I am using dhtmlx tabbar to display the child. code:`tabbar.setHrefMode("iframes-on-demand"); tabbar.setContentHref("a1", "../../MailContactMain/Index");` then will i use a1 as iframe – Jonathan Dec 19 '12 at 13:36
  • Oh, I never used that before. But there has to be an iframe or frame. Without a frame the property window.parent is the same link just window (window.parent == window) => true – algorhythm Dec 19 '12 at 13:38
  • If you are in a frame, then window.parent is the frame-holder-document. If you are in the frame-holder-document, then use window.frames[0] OR window.frames[1] to switch in the frame-context. Look here: http://de.selfhtml.org/javascript/objekte/frames.htm – algorhythm Dec 19 '12 at 13:49
3

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()');
}​
palaѕн
  • 72,112
  • 17
  • 116
  • 136
3
var win = null;
function openAndCall(id){
    if (win!=null && win.closed==false){
        win.close();
    }
    win = window.open("/somePage");
    win.onload = function(){
        win.callSome(id);
    };
}
Qantas 94 Heavy
  • 15,750
  • 31
  • 68
  • 83
Tito100
  • 1,660
  • 1
  • 12
  • 12