Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you override the window.location error event?

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...

mobile safari canno open page

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.

like image 357
JoJo Avatar asked Jan 06 '12 22:01

JoJo


People also ask

What is window location replace?

Window location. The replace() method replaces the current document with a new one.

What is window location href?

Window Location Href The window.location.href property returns the URL of the current page.

What does Window location assign do?

The Location. assign() method causes the window to load and display the document at the URL specified. After the navigation occurs, the user can navigate back to the page that called Location.


1 Answers

Your solution is here.

Basically, you can use cookies to determine wether your application is installed or not.

like image 91
ldiqual Avatar answered Sep 28 '22 07:09

ldiqual