The general problem
In Javascript, I need to be notified when there's an error loading a URL and override the default behavior. For example, executing the following on iOS Safari...
window.location = 'http://pageDoesNotExist.badFormatting';
... will pop up this alert message...
I would like attach a listener for such an error and do something instead of showing an ugly alert box.
My specific problem
When a user taps a button, I need to launch my app if it is installed, else I need to open the App Store for her to download my app. The accepted solution is:
// Attempt to open app
window.location = 'myApp://';
// If app fails to open, will open app store 0.5 seconds later
window.setTimeout(
function() {
window.location = 'http://itunes.apple.com/myAppId';
},
500
);
The problem here is that the first window.location
opens the ugly alert box when the app is not installed. The code will then fallback to the App Store. When the user returns from the App Store back to Safari, the ugly alert box is still there. As far as I know window.onerror
doesn't fire on mobile Safari.