This function takes a Custom Protocol URL as parameter. It will then create a temporary <iframe> where the passed URL is loaded via a simulated click on a hidden <a> element.
function invokeProtoLink(url) {
return new Promise(function(resolve, reject) {
// create temp frame where url will be opened
const frame = document.createElement('iframe');
frame.name = '_invoker_' + Math.random();
// create temp link and set it's target to temp frame
const lnk = document.createElement('a');
lnk.href = url;
lnk.target = frame.name;
// frame must be appended to body otherwise link will
// open in new tab, and might trigger popup blocker
document.body.appendChild(frame);
setTimeout(function() {
// remove temp frame
frame.parentNode.removeChild(frame);
resolve();
}, 0);
// a simple lnk.click() did not work in firefox
// hence we're using dispatchEvent
lnk.dispatchEvent(new MouseEvent('click'))
});
}
Usage based on your example would be: (this must be within an event handler such as onclick)
invokeProtoLink('abcd://').then(() => {
invokeProtoLink('xyz://');
});
Tested on Chrome 68, Edge 42, and Firefox 61.