0

I'm working in Moodle and I have multiple activities that open in a new window through the file or SCORM activities. I'm trying to refresh the current page, when any new window is closed.

Something like this:

if(window is closed) { 
    window.location.reload()
}

But I know that doesn't work because window.open just opens a window it doesn't test if the window is already open and.

Nick0989
  • 459
  • 5
  • 15

2 Answers2

1

On close of a pop up window, you want to reload the page that opened it. So on close of the pop up, reload the opener

window.addEventListener('unload', function () {
    window.opener.location.reload();
});
epascarello
  • 204,599
  • 20
  • 195
  • 236
  • `unload` does not mean the window was closed though, only that the document inside it was unloaded ... for example by navigation inside the popup. – misorude Dec 04 '18 at 15:42
  • does it hurt it is reloads if user refreshes the pop up? Other option is the user polls to see if the pop up is closed. There is not much one can do since there is no real event that tells them this state. – epascarello Dec 04 '18 at 15:49
  • This has no affect on closing a window unfortunately. Why it was marked as duplicate is incorrect imo. There are multiple windows being opened on the same page within a Moodle course. I should have mentioned that. – Nick0989 Dec 04 '18 at 16:05
0

Store a reference to the opened window and set an interval to periodically check it's closed property

var newWindow = window.open(url);
var interval = setInterval(function(){
    if(newWindow.closed){
        clearInterval(interval);
        window.location.reload();
    }

},1000);// one second interval
charlietfl
  • 170,828
  • 13
  • 121
  • 150