-1

I have problem that I can't call function of parent window in child window.

var value = "test";
function drawChart() {
    myWindow = window.open("", "Chart", "width=500, height=500");
    myWindow.console.log(parent.value);
    myWindow.parent.createGraph();
}

function createGraph(){
  ...
}

I can access global variable value, but not call function createGraph. I am getting error:

Uncaught TypeError: myWindow.createGraph is not a function

I moved all javaScript in head, but still not working.

Rokas.ma
  • 191
  • 1
  • 12
  • 1
    There are few things I cannot understand: from the parent window you are calling myWindow.parent.createGraph; but createGraph is in parent so why not call it directly in the parent. Moreover the error you have "Uncaught TypeError: myWindow.createGraph is not a function" is strange because it seams you are calling "myWindow.createGraph()" instead of myWindow.parent.createGraph(). Can you show a jsfiddle ? – pinturic Jul 21 '15 at 12:46
  • `myWindow` is your *child* window, not your parent, right? Try [this](http://stackoverflow.com/q/1777864/1267304). – DontVoteMeDown Jul 21 '15 at 12:47
  • @DontVoteMeDown yap it is child window – Rokas.ma Jul 21 '15 at 12:56
  • Please provide a demo on [jsFiddle](https://jsfiddle.net), [CodePen](https://codepen.io), [jsBin](https://jsbin.com), or some other equivalent site. – zer00ne Jul 21 '15 at 13:49

1 Answers1

1

I think you're after:

window.opener: Returns a reference to the window that opened this current window

So, for example, within your popup window, you call your function like:

window.opener.createGraph();

Edit: Of course it wouldn't work with the way you've been trying! You need to call the parent function within the scope of the opened child window. What you're doing is calling them within the same function that triggered the child window and NOT within the child window.

Here is a hacky way along the same lines of your code. Bear in mind, this is only tested on Chrome.

$("button").on("click", function() {
    var win = window.open("", "Chart", "width=400,height=400");
    win.console.log("New window");
    win.callFunc = function(fn) {
        win.console.log(fn());
    }
    win.callFunc(aParentFunction);   
});

function aParentFunction() {
    return "Hiya, I am your dad.";
}

A Demo

lshettyl
  • 8,166
  • 4
  • 25
  • 31