3

If I do this:

var new_win = window.open();

How do I make it so that all of the functions that could be used in the parent window can now be used in the child window (new_win)?

I do not want to do:

var fun1 = window.opener.fun1;
var fun2 = window.opener.fun2;
...
Naftali
  • 144,921
  • 39
  • 244
  • 303
  • Do you have any dynamically generated functions and you want those included as well? Otherwise, why not include the same javascript files in the opened window as well (using newWin.document.write if you don't have a location). – deviousdodo Nov 01 '11 at 14:49
  • @draevor all of the functions are dynamic based on the user click on the parent window. – Naftali Nov 01 '11 at 14:50

3 Answers3

5

Please note that what follows is a dangerous, dirty, messy, M$-level hack. I am fully aware of this, but it (theoretically) does what @Neal wants. (I'm a little scared to even post it, and I fully expect downvotes)

var i, w = window.opener;
for (i in w)
{
    if (w.hasOwnProperty(i) && !window.hasOwnProperty(i) && typeof w[i] === 'function')
    {
        window[i] = w[i];
    }
}

In light of the scope issues, I've determined that we must use .bind. There is a shim at the MDN Entry for Function.bind which will be necessary for certain browsers.

Please note that before using .bind, the code must check to see if the property is a function. I have done this along with the hasOwnProperty checks, but if you wish to transfer values as well as functions, you may want to do this within its own if statement.

var i, w = window.opener;
for (i in w)
{
    if (w.hasOwnProperty(i) && !window.hasOwnProperty(i) && typeof w[i] === 'function')
    {
        window[i] = w[i].bind(window);
    }
}
Ryan Kinal
  • 17,414
  • 6
  • 46
  • 63
  • Note that all functions are still being executed in the scope of the original window. – Rob W Nov 01 '11 at 14:51
  • @RobW not if this is inside of `new_window.onload = function(){...above code...}` i believe (using `this` or `new_window` instead of `window`) – Naftali Nov 01 '11 at 14:52
  • 1
    When I use `function foo(){alert(document.title)};var w = window.open('about:blank');w.onload=function(){w.foo=foo}`, and use `foo()` at the opened window, I get the title of the original document. – Rob W Nov 01 '11 at 14:54
  • I see no issue with that. That is just what should happen. – Naftali Nov 01 '11 at 14:55
  • This should be executed in the *child* window, not in the parent. – Ryan Kinal Nov 01 '11 at 15:18
1

First off, I would create a namespace (helps avoid function collisions) and then I would just reference it in the popup window.

parent window:

MyNameSpace = {
      // put functions, classes or whatever you want in here.
};

in the pop-up window:

MyNameSpace = window.opener.MyNameSpace;

The only issue potentially with what you're asking for is if the function you call is trying to reference the window object. I would pass a window object to any functions that manipulate a window.

e.g.

function (arg1, arg2, argn, windowHandle) {
      windowHandle = windowHandle || self;

      // do some stuff.

}
nickytonline
  • 6,855
  • 6
  • 42
  • 76
0

You could create an new instance of a function and pass the function name and optional argNs something like as follows

var parentFunction = new Function("name", "args", "return (window.opener != null) ?  opener.window[name](args) : false");

parentFunction('notification','waring, red, 2');
david
  • 4,218
  • 3
  • 23
  • 25