7
  1. I have registered two protocols.
  2. When i try to invoke both protocols in same event,only one invoke at a time in Chrome browser.

$(function () {
    $("div[href]").click(function (event) {
        debugger;
        //for validation purpose.
        window.location = "abcd:";

       //if it is validated then
        window.location ="xyz:";



    });
});
<!DOCTYPE html>
<html>

<head lang="en">
    <meta charset="UTF-8">
    <title>Custom Protocol Detection</title>
</head>

<body id="abcd">
    <h1>Click one of these labels:</h1>
    <a href="#" id="atemp"></a>
    <div href="blahblah:randomstuff"  style="background-color:aquamarine">
        Non-exist protocol
    </div>
    <div href="mailto:johndoe@somewhere.com" style="background-color:aqua">
        Send email
    </div>
    <script src="https://code.jquery.com/jquery-1.11.2.min.js"></script>   
    <script src="example.js"></script>
</body>

</html>
  1. Please let me know how do i invoke both protocols in same event.
  • Might be missing something, but since a window can only be at one location at any given time, only one 'protocol' as you call it can be assigned to `window.location`. You can do multiple ajax-calls at the same time though – Geert-Jan Aug 19 '18 at 11:55
  • Are you talking about `window.open()` (or as Geert said multiple ajax calls)? `window.location` is current window's address, assigning twice is meaningless. – MT-FreeHK Aug 20 '18 at 01:35
  • @Geert-Jan First of all create two different Protocols.And to invoke that protocols if you will assign to Window.Location it invoke.I do the same first protocol i passed to validate and second to invoke application. – rishabh gupta Aug 20 '18 at 06:30
  • @Geert-Jan Here OP is trying to show an example of invoking multiple protocols in a single call. It can also be done by creating dynamic links and clicking them programmatically. [Here](https://stackoverflow.com/a/20247798/3085520) is an example to invoke custom protocol using dynamic anchor tags. – prem Aug 20 '18 at 06:50
  • This is kinda like asking why you are not successful in stepping onto two omnibuses at once … – CBroe Aug 20 '18 at 07:56
  • @CBroe The requirement is simple. If the protocol is registered then do something otherwise do something else and the issue is with Chrome browser only. – prem Aug 20 '18 at 08:41
  • 1
    @HyyanAboFakher It makes complete sense for the ones facing this problem. If you any suggestions then please help. – prem Aug 24 '18 at 12:09
  • https://stackoverflow.com/questions/2872090/how-to-check-if-a-custom-protocol-supported – Hyyan Abo Fakher Aug 24 '18 at 12:50
  • https://gist.github.com/rajeshsegu/3716941/f756f6e3345fb9b06236e465363882838baf4fd1 – Hyyan Abo Fakher Aug 24 '18 at 12:50
  • https://github.com/ismailhabib/custom-protocol-detection – Hyyan Abo Fakher Aug 24 '18 at 12:51
  • @HyyanAboFakher Thanks for sharing the links but we have already seen all these. We are already using ismailhabib detection library. It works great with all browsers but the scenario in which we stuck occurs with this library also. Actually on chrome there is not good way to detect custom protocol, all these library uses blur event which is kinda workaround but works in most scenarios. – prem Aug 25 '18 at 12:07
  • Also on invoking protocol multiple time in a single javascript call other browser like IE and Firefox works fine but chrome behavior is random.[Here](https://stackoverflow.com/questions/23096887/how-to-start-two-or-more-custom-url-protocol-from-javascript) you can find more details of the problem we are facing. – prem Aug 25 '18 at 12:08
  • Having spent the an interesting, though largely wasted, hour or so reading through whatwg and the html5 spec, I strongly suspect you are dabbling in areas which are not well defined across browsers, so I'm not sure its fair to say that Chrome is behaving badly. I would suggest you restate your question in terms of what you are actually trying to accomplish - like why you are using custom schemes, and what the two actually do and what you expect to happen - and perhaps you will get a better answer that will reliably work across major browsers. – wordragon Aug 26 '18 at 06:08
  • Also, Chrome states that they support the navigator custom registration, but require all custom protocols to start with "web+". Got that from [caniuse](https://caniuse.com/#search=navigator.register), but remember reading about that in google's docs awhile back. Not sure if that is part of your issue. – wordragon Aug 26 '18 at 06:17

1 Answers1

0

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.

lostsource
  • 21,070
  • 8
  • 66
  • 88
  • A simulated click on a simulated custom protocol - sweet! +1 for counterpoise – wordragon Aug 27 '18 at 03:46
  • @lostsource your code is working only when debugging mode is on.Once i close inspect element it does not work. – rishabh gupta Aug 27 '18 at 05:56
  • @rishabhgupta does this happen in all browsers? did you try removing the `debugger;` statement? – lostsource Aug 27 '18 at 10:20
  • @lostsource i mean to say when i do inspect and go through the code by F10 its work fine.And if i use without inspect it does not work.And i used Chrome Version 68.0.3440.106 (Official Build) (64-bit). – rishabh gupta Aug 28 '18 at 05:16
  • @rishabhgupta please show me the code where you're calling the `invokeProtoLink` method – lostsource Aug 29 '18 at 19:44
  • @lostsource sorry for delay.Please go to the link http://plnkr.co/edit/aF4g2vgL72dlDSJiN2cI?p=preview to see my implementation. – rishabh gupta Sep 12 '18 at 06:55