title: How do you override the window.location error event?
body: <h1>The general problem</h1>
  <p>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...</p>
  <pre><code>window.location = 'http://pageDoesNotExist.badFormatting';
  </code></pre>
  <p>... will pop up this alert message...</p>
  <p><img src="https://i.stack.imgur.com/WaHFm.jpg" alt="mobile safari canno open page" /></p>
  <p>I would like attach a listener for such an error and do something <em>instead</em> of showing an ugly alert box.</p>
  <h1>My specific problem</h1>
  <p>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 <a href="https://stackoverflow.com/questions/1108693/is-it-possible-to-register-a-httpdomain-based-url-scheme-for-iphone-apps-like/1109200#1109200">accepted solution</a> is:</p>
  <pre><code>// 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
  );
  </code></pre>
  <p>The problem here is that the first <code>window.location</code> 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 <code>window.onerror</code> doesn't fire on mobile Safari.</p>