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.

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. Commented Dec 19, 2012 at 13:22
  • 1
    No i am not having any iframe Commented Dec 19, 2012 at 13:28
  • if you're using window.parent and it's working, then you have an iframe. 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 Commented Dec 19, 2012 at 13:30

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

Comments

4

Do you have an iframe?

Do something like that:

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

5 Comments

I am not having any frames. Then how can i do that
@Naidu And where or what is your child? When you make window.parent in which context are you? Give me more code?!
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
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
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: de.selfhtml.org/javascript/objekte/frames.htm
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()');
}​

Comments

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);
    };
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.