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
...
}
As stated on MDN's Window.open() page:
Return value
A
WindowProxyobject, which is basically a thin wrapper for theWindowobject representing the newly created window, and has all its features available. If the window couldn't be opened, the returned value is insteadnull.
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.