0

I have some problems with the function to refresh the main page when a pop is closed.

In my popUp page I have the following javascript

$(function () {        
    window.onunload = function () {
        if (window.opener && !window.opener.closed) {
            window.opener.popUpClosed();
        }
    };
});

In the main page I have the following javascript

$(function () {
    function popUpClosed() {
        window.location.reload();
    }
});

I got an error, that the object don't support the method 'popUpClosed'.

I saw this solution on this answer here in StackOverflow, but I don't know how can I fix this error.

Community
  • 1
  • 1
Lucas_Santos
  • 4,638
  • 17
  • 71
  • 118

2 Answers2

0

Have you tried the next answer down:

var myPop = "pop up window selector"
myPop.onunload = function(){ 
  location.reload(); 
};

Source: Refresh parent window when the pop-up window is closed

Update

You can get a reference to a window like this:

var windowObjectReference = window.open(strUrl, strWindowName[, strWindowFeatures]);

Source: https://developer.mozilla.org/en/DOM/window.open

Update

I just realized that window.opener.popUpClosed() is looking for the popUpClosed() function in the global scope on the main page, but it's not actually declared in the global scope, try this:

var popUpClosed = function () {
    window.location.reload();
};

Removing the document.ready event handler around this will allow it to be accessible from a child document (like a popup). Also notice the function is now declared like: var <name> = function () { ... }; which allows it to be accessible via the window object.

Community
  • 1
  • 1
Jasper
  • 75,717
  • 14
  • 151
  • 146
0

It's because window.opener does not have any method called popUpClosed. Remove the window.opener as such:

window.onunload = function () {
  if (window.opener && !window.opener.closed) {
    popUpClosed();
  }
};
Nick Beranek
  • 2,721
  • 1
  • 19
  • 14
  • `window.opener` is a reference to the `window` object for the document that opened the current document. So `window.opener` is like using `window` for a parent document. – Jasper Feb 28 '12 at 19:45
  • I know what `window.opener` is. I also know that it does not have a `popUpClosed` method. He would need to extend `window.opener`. – Nick Beranek Feb 28 '12 at 20:02
  • But how can I reload a specific page ? The Parent page and another one ? – Lucas_Santos Feb 28 '12 at 20:03
  • @NickBeranek When you declare anything in the global scope, you can access it via the `window` object. For instance: `var key = 'val'; alert(window.key)` will output `val`. So you can declare a function: `function key () { return 'val'; }` and run its code like this: `window.key();`. So you can add a function to the `window` object and access it from a different document. Here is a demo: http://jsfiddle.net/jasper/xZYNk/1/ – Jasper Feb 28 '12 at 20:07
  • Instead of `window.location.reload()`, replace it with `window.opener.location.reload()`. – Nick Beranek Feb 28 '12 at 20:08
  • @Jasper, `popUpClosed` is NOT chained to `window.opener`. You'd have to declare `var popUpClosed = function() {};` in order to access it from `window`. – Nick Beranek Feb 28 '12 at 20:09