views:

34

answers:

1

Can I wrap a try/catch around the opening of a URL in mobile safari? I know the question has been asked a number of different ways but essentially what I'm after is the trapping of bad URLs or the detection of when a specific URL cannot be opened by the browser.

+1  A: 

If the URL is within the same domain, then you can use an XMLHttpRequest to determine if it returns a valid response.

e.g

var xhr = new XMLHttpRequest();

function handler(){
  if (xhr.readyState != 4)  { return; }
  if (xhr.status != 200) { return "THIS IS A BAD LINK"}
}

if (xhr != null) {
    xhr.open("GET", URL_YOU_ARE_TESTING, true);
    xhr.send();
    xhr.onreadystatechange = handler;
}

However this won't work for links to other sites (which is what I guess you want) due to the browser's same origin policy. I don't believe there is a javascript solution for this case.

DanSingerman
Thanx for the response but I should have clarified that I'm trying to launch an installed app with the URL and detect when the app is not installed. I believe it boils down to detecting a bad URL but I don't know because I never really dealt with mobilesafari before.
Cliff
Check out this answer - I think it does what you want: http://stackoverflow.com/questions/627916/iphone-safari-check-if-url-scheme-is-supported-in-javascript/1404662#1404662
DanSingerman