htmlbrowsermodernizrfeature-detectionuri-scheme

Detecting URI scheme support in-browser


Is there a Modernizr-like way to detect client support for uri schemes such as <a href="sms:1-408-555-1212">New SMS Message</a> or <a href="whatsapp://send?text=Foo">New WhatsApp Message</a> in order to conditionally display such links?

Possibly related

Check if URL scheme is supported in javascript

Is there something similar to iOS's canOpenURL to check URL schemes for mobile browsers?


Solution

  • There are a few tricks you can use, but if you have to deal with IE < 11 or even IE 11 on Windows earlier than Windows 8, you won't like my response.

    Generally, there is no way to detect support for the URL-scheme if following these URLs has a side-effect.

    What you can do is try to open one of these URLs in an iframe. So you would append an iframe to the body (or wherever you want to) and then set its href to the special url.

    In Mozilla browsers, you will get an exception if the URL scheme isn't supported. In Webkit based browsers (and Chrome), nothing will happen.

    So unless you're on Mozilla, there's not even a way to find out whether why you've done has just worked or not.

    In IE11 under Windows 8 and later (but not under earlier versions of Windows - there's the method is just missing), you can use window.natigator.msLaunchUri to launch that special url. This too will throw if the URI scheme is not supported.

    Unfortunately, and this might really put a dampener on what you want to do: No matter what trick you use: If the scheme it not supported and you try to launch it on any version of IE that doesn't support msLaunchUri, your page will be replaced by an error page and there is no way to prevent this from happening.

    I've tried everything. The iframe trick causes the error message. Trying window.open causes the error message or does nothing depending on the weather. Setting an img URL doesn't cause the error (good) but also doesn't cause the URL to actually be launched (bad).

    Unless you have cooperation of the application who's url you're going to launch (for example by telling IE to pollute the User-Agent header if the app is installed), you are out of luck in IE.

    Yeah - very likely not what you wanted to hear, I'm sorry.