1

How to check the status and/or wait until a successful connection of a window.open()? (Or suggest better alternatives)

var url = 'https://www.google.com';
var newWindow = window.open(url, 'main');

if(newWindow == 'success'){ //url was successfully opened
    ...
}
else { //url returned 404
    ...
}
  • Does this answer your query ? [Check if window is already open window.open](https://stackoverflow.com/q/12138236/15405352) – Anil Parshi Oct 05 '21 at 05:12

1 Answers1

2

As stated on MDN's Window.open() page:

Return value

A WindowProxy object, which is basically a thin wrapper for the Window object representing the newly created window, and has all its features available. If the window couldn't be opened, the returned value is instead null.

So, it suffices to check for null:

var url = 'https://www.google.com';
var newWindow = window.open(url, 'main');

if (newWindow == null) {
  // Failed
} else {
  // Success
}

A window being successfully opened doesn't give you information about the HTTP response status of the URL loaded in that window though.

Robby Cornelissen
  • 91,784
  • 22
  • 134
  • 156