Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
292 views
in Technique[技术] by (71.8m points)

javascript - simplest cross-browser check if protocol handler is registered

When user clicks link with custom protocol (like myapp://superlink)

I need either launch an app or allow user to download and run configuration app

I am looking for cross-browser way to check if custom protocol is registered

I've tried to determine this by checking user agent server-side (for IE)

[HKEY_LOCAL_MACHINESOFTWAREWow6432NodeMicrosoftWindowsCurrentVersionInternet Settings5.0User AgentPost Platform] "myapp"=""

sends

`....NET CLR 3.0.30729; .NET4.0C; .NET4.0E; InfoPath.3; **myapp**`

as user-agent

This is good, clean way, easy configuration:

just download .reg file and run it or propagiate via ms windows policy

I can't fix this for Chrome and Firefox

Are there any client-side solution (in js)?

My enviroment: IE8+, Chrome (latest), Firefox(latest)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There is this old tricks that it always never fails me.

The core functionality that you need is setTimeout. I will tell you in detail:

setTimeout(function() {
  window.location = "http://itunes.com/app/yourapplocation";
}, 200);

// once you do the custom-uri, it should properly execute the handler, otherwise, the settimeout that you set before will kick in
window.location = "myapp://superlink";

Now you mentioned that it maybe a link or links so I made this nice function just for your convenience:

HTML code

<a href="myapp://superlink" data-href-alt="http://itunes.com/app/yourapplocation">Click here</a>

JS code

$("a[href*='myapp://']").click(function(e)
{
  var el = $(this);
  setTimeout(function() {
    window.location = el.data("data-href-alt");
  }, 200);

  // once you do the custom-uri, it should properly execute the handler, otherwise, the settimeout that you set before will kick in
  window.location = el.data("href");

  e.preventDefault();
});

Hope this will help you :)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...