how do I detect if window.location failed?

Sagi Mann picture Sagi Mann · Aug 23, 2013 · Viewed 16.4k times · Source

how do I check if a call to window.location failed because the given URL was invalid, etc? Is there some event I can set on the window object or on some other object that can catch this?

Answer

Sagi Mann picture Sagi Mann · Sep 5, 2013

Finally got it to work using a "workaround" that is not a generic solution as I originally hoped:

I am using the fact that the link I am trying to open is a custom url scheme (e.g. myxx://localhost) on mobile, and if it fails, the action I want to perform is a redirection to a standard appstore URL (os-specific). The workaround tries to open the custom URL, and if it fails, the timeout function kicks in shortly after, and opens an alternative url:

setTimeout(function() { window.location=alternateUrl; }, 25);
window.location = customUrl;

The downside is that when the customURL fails, a standard safari browser shows a message box that the site could not be opened, but at least it still redirects the user to the appstore.