How do I detect if window.location failed?

17,721

Solution 1

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.

Solution 2

Not really possible, because when window.location = someURL is executed, before the URL is even tested, your document is removed from the window. You have no code remaining that could test if it worked.

If the link is on the same origin, you may issue an XMLHttpRequest to test if the page is reachable but there doesn't seem to be a way to test if a page isn't requestable just due to cross origin request or because the URL is wrong.

For a general document, I don't know any way to test if a foreign origin page is reachable (but it can be done for an image using the onload event handler).

Solution 3

you can check if page exists using ajax. didn't test code, but it should work.

var rekuest= new XMLHttpRequest();  
rekuest.open('GET', 'http://www.thisdoesnotexits.org', true);  
rekuest.send();  
if (rekuest.status === "404") {alert("not exist!"); }  
Share:
17,721

Related videos on Youtube

Sagi Mann
Author by

Sagi Mann

Summary: 24 years experience in the hi-tech industry, out of which 21 years experience in dev management, leadership and programming. 4 years experience in IT Management 7 years experience in IT Courses administration and instruction Specialties: Enterprise Cloud & Mobile architecture Project management & leadership Web technologies (JS/NodeJS, HTML5, PHP, ...) Java technologies (JSE, JEE, Hibernate, ...) Python ObjC C/C++ C# I18N

Updated on October 17, 2022

Comments

  • Sagi Mann
    Sagi Mann over 1 year

    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?

  • Sagi Mann
    Sagi Mann almost 11 years
    I tried with XMLHttpRequest, but the url is not in the same domain, so it doesn't work...
  • Michael Todd
    Michael Todd almost 11 years
    @sagimann You could set up a server application that you could call from your app that would check it. That way you won't hit a cross origin error.
  • Denys Séguret
    Denys Séguret almost 11 years
    Yes, that's not really the answer to "how to do it in javascript" but it really is the most reasonable solution.
  • SpringLearner
    SpringLearner over 4 years
    @someUser I have tested now in android and it worked