2

I've created a popup window and in it I'd like to use a function I've created in the parent window. I've tried using window.opener.parentFunction() but to know avail. Has anyone else experienced this problem? Here's what my code looks like.

function parentFunction(){
alert('testing');
}


function print(){
var new_win = window.open('','name','height=400,width=500');
var output = "<html><head></head><body><table>\n";


for (i = 0; i < 10; i++) {
output += "<td onclick='window.opener.parentFunction()'>"+i+"</td>";
}

output += "</table></body></html>\n";
new_win.document.write(output);
}

*Got it working. Thanks guys.

3
  • In the new window, is window.opener null? Is parentFunction visible at the global scope? How is print() bound? Are you sure that this.getCols() is defined? What about years? The code you posted is not complete.
    – Matt Ball
    Mar 22, 2011 at 14:01
  • Sorry about that. I didn't include all my code. I just edited it to make it simpler. Mar 22, 2011 at 14:12
  • your code now work as-is.. do you still have any question or problem? If not please mention that it's now fixed in the question body as well.. Mar 22, 2011 at 14:32

1 Answer 1

4

There are a bunch of problems with your code. I've put together a working demo here.

HTML

<button id="clickMe">Click me</button>

JavaScript

window.onload = function() {

    function parentFunction() {
        alert('testing');
    }

    window.parentFunction = parentFunction;

    var years = [1, 2, 3, 4, 5, 6];

    function print() {
        var new_win = window.open('', 'name', 'height=400,width=500');
        //var cols = this.getCols();
        var cols = 2;
        var output = "<html><head></head><body><table>";
        var cell_count = 1;
        for (i = 0; i < years.length; i++) {
            if (cell_count === 1) {
                output += "<tr>";
            }
            output += "<td onclick='window.opener.parentFunction();'>" + years[i] + "</td>";
            cell_count++;
            // end the row if we've generated the expected number of columns
            // or if we're at the end of the array
            if (cell_count > cols || i === years.length - 1) {
                output += "</tr>\n";
                cell_count = 1;
            }
        }
        output += "</table></body></html>";
        new_win.document.write(output);
    }

    document.getElementById('clickMe').onclick = print;
};
  • years was not defined
  • this.getCols() was not defined
  • parentFunction was (probably) not visible at the window scope
12
  • @Matt window.addEvent - is this a library? Mar 22, 2011 at 14:21
  • @Šime: that's just what jsfiddle does behind the scenes by default - I guess MooTools? I threw it in there mostly to show how parentFunction might not be visible in the global scope, but you're right, that ain't vanilla JS. Will tweak.
    – Matt Ball
    Mar 22, 2011 at 14:24
  • Exactly what it looks like. Assigns whatever value parentFunction evaluates to (in this case, a function) to a property of the window object, also named parentFunction.
    – Matt Ball
    Mar 22, 2011 at 14:34
  • I was actually able to see it working without that line: jsfiddle.net/yahavbr/Nvgq9 so it's probably something else you changed that fixed the problem.. Mar 22, 2011 at 14:34
  • 2
    @Shadow That depends on the options in jsfiddle. If you use "no wrap" then parentFunction is a global function. If you use onDomReady or onLoad then it is a local to the ready/load handler. Mar 22, 2011 at 14:36

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.