Skip to content

Instantly share code, notes, and snippets.

@aaronk6
Last active November 9, 2023 05:17
Show Gist options
  • Save aaronk6/d801d750f14ac31845e8 to your computer and use it in GitHub Desktop.
Save aaronk6/d801d750f14ac31845e8 to your computer and use it in GitHub Desktop.
launchUri

Cross-browser implementation of navigator.msLaunchUri

Microsoft’s navigator.msLaunchUri method only works in Internet Explorer on Windows 8. Therefore I came up with a (nearly) cross-browser implementation that uses the native msLaunchUri when it’s available and falls back to adventurous hacks when running in other browsers.

Description

launchUri (uri, successCallback, noHandlerCallback, unknownCallback)

If a default protocol handler is available on the system that matches the URI, the successCallback is invoked, otherwise, the noHandlerCallback is called. This works in the following browsers:

  • Internet Explorer 8-11 (tested on Windows 7 and Windows 8)
  • Chrome (tested with v. 39 on OS X and Windows)
  • Firefox (tested with v. 34 on OS X and Windows)

In all other browsers, the URI will be launched but you cannot find out if it worked (the unknownCallback is invoked).

Example:

launchUri('x-my-app://example-string', function () {
	// SUCCESS - the protocol is registered and the user was asked to open
	// the URI in the appropriate application
	alert('Have fun with my app');
}, function () {
	// FAILURE - the protocol isn't registered
	alert('Y u no install my app?');
}, function () {
	// UNKNOWN - we don't know wether the protocol is registered or not
	alert('Hey, did my app launch? If not, please install it. kthxbye');
});

License

MIT

Credits

/*!
* Copyright © 2015 aaronk6
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
function launchUri (uri, successCallback, noHandlerCallback, unknownCallback) {
var res, parent, popup, iframe, timer, timeout, blurHandler, timeoutHandler, browser;
function callback (cb) {
if (typeof cb === 'function') cb();
}
function createHiddenIframe (parent) {
var iframe;
if (!parent) parent = document.body;
iframe = document.createElement('iframe');
iframe.style.display = 'none';
parent.appendChild(iframe);
return iframe;
}
function removeHiddenIframe(parent) {
if (!iframe) return;
if (!parent) parent = document.body;
parent.removeChild(iframe);
iframe = null;
}
browser = { isChrome: false, isFirefox: false, isIE: false };
if (window.chrome && !navigator.userAgent.match(/Opera|OPR\//)) {
browser.isChrome = true;
} else if (typeof InstallTrigger !== 'undefined') {
browser.isFirefox = true;
} else if ('ActiveXObject' in window) {
browser.isIE = true;
}
// Proprietary msLaunchUri method (IE 10+ on Windows 8+)
if (navigator.msLaunchUri) {
navigator.msLaunchUri(uri, successCallback, noHandlerCallback);
}
// Blur hack (Chrome)
else if (browser.isChrome) {
blurHandler = function () {
window.clearTimeout(timeout);
window.removeEventListener('blur', blurHandler);
callback(successCallback);
};
timeoutHandler = function () {
window.removeEventListener('blur', blurHandler);
callback(noHandlerCallback);
};
window.addEventListener('blur', blurHandler);
timeout = window.setTimeout(timeoutHandler, 500);
window.location.href = uri;
}
// Catch NS_ERROR_UNKNOWN_PROTOCOL exception (Firefox)
else if (browser.isFirefox) {
iframe = createHiddenIframe();
try {
// if we're still allowed to change the iframe's location, the protocol is registered
iframe.contentWindow.location.href = uri;
callback(successCallback);
} catch (e) {
if (e.name === 'NS_ERROR_UNKNOWN_PROTOCOL') {
callback(noHandlerCallback);
} else {
callback(unknownCallback);
}
} finally {
removeHiddenIframe();
}
}
// Open popup, change location, check wether we can access the location after the change (IE on Windows < 8)
else if (browser.isIE) {
popup = window.open('', 'launcher', 'width=0,height=0');
popup.location.href = uri;
try {
// Try to change the popup's location - if it fails, the protocol isn't registered
// and we'll end up in the `catch` block.
popup.location.href = 'about:blank';
callback(successCallback);
// The user will be shown a modal dialog to allow the external application. While
// this dialog is open, we cannot close the popup, so we try again and again until
// we succeed.
timer = window.setInterval(function () {
popup.close();
if (popup.closed) window.clearInterval(timer);
}, 500);
} catch (e) {
// Regain access to the popup in order to close it.
popup = window.open('about:blank', 'launcher');
popup.close();
callback(noHandlerCallback);
}
}
// No hack we can use, just open the URL in an hidden iframe and invoke `unknownCallback`
else {
iframe = createHiddenIframe();
iframe.contentWindow.location.href = uri;
window.setTimeout(function () {
removeHiddenIframe(parent);
callback(unknownCallback);
}, 500);
}
}
@ssirois
Copy link

ssirois commented Feb 27, 2017

@aaronk6 Thank you for the follow up. Since the Expat License (a.k.a the MIT license) is compatible with the GNU/GPL License so all good on my side! 👍

This is really appreciated.

Thank you for your time and sorry for the Twitter harassment. 😉 I think Gist messages leave less footprint than issues&features comments inside a GitHub project, therefore the ping on Twitter.

@aaronk6
Copy link
Author

aaronk6 commented Feb 28, 2017

@ssirois Glad I could help!

BTW, just double-checked: Comments below Gists indeed don’t trigger any email notifications. And we’re not the only ones wondering about this: isaacs/github#21 Really surprising.

@mafar
Copy link

mafar commented Apr 11, 2017

@aaronk6
Here is what I did. I registered a protocol but I renamed application folder to see if i get FAILURE or UNKNOWN
Results:

  1. IE 11 ==> FAILURE
  2. Chrome ==> SUCCESS (this is a problem)
  3. Firefox ==> UNKNOWN
  4. Edge ==> SUCCESS (this is a problem)
          launchUri(href, function() {
            console.log('SUCCESS');
          }, function() {
            console.log('FAILURE');
          }, function() {
            console.log('UNKNOWN');
          });

In this special case, can we get UNKNOWN for all browsers since protocol is registered but app can not be executed since it is not there.
But the real problem is with chrome and Edge with go into success state.

This library https://github.com/ismailhabib/custom-protocol-detection has same problem with chrome as your gist has. goes into success callback

@mafar
Copy link

mafar commented Apr 11, 2017

@aaronk6
I noticed if i bring down timeout to 10 from 500 then it is ok for chrome means it gives failure ???
timeout = window.setTimeout(timeoutHandler, 10); // Blur hack (Chrome)

@katsuya0515
Copy link

Hi
First of all, thanks for the awesome work!
I was struggling to open a word file using ms-word scheme (https://stackoverflow.com/questions/47831690/ms-word-url-scheme-does-not-work-from-chrome-browser-on-mac-os), and assumed your work could help me out of this.

I tried this with your code but still not be able to get it working. Have you happened to try your code with ms-word scheme?

@katsuya0515
Copy link

solved this myself.....it was just a problem on my end, I couldn't get this working because I was giving a url like

ms-word:ofe|u|'your-webdav-url.

if you simply escape letters

ms-word:ofe%7Cu%7C'your-webdav-url'

It'll work amazingly with your code. Thanks!

@abhishekdana1999
Copy link

launchUri is not defined

@mystery3136
Copy link

When I check the box for always allow to open these type of links in associated apps then chrome blur method doesn't work. It opens the associated app and since focus is not stolen it also gives the fail call back. @aaronk6

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment