Is it possible to test whether a user's browser/OS supports a given type of link using javascript?

ぐ巨炮叔叔 提交于 2019-12-10 14:07:13

问题


Is it possible to test whether a user's OS/browser supports a given url scheme using javascript (or anything else)?

For example, mailto: isn't setup on most user's computer that only use webmail. Would it be possible to somehow catch attempts to click a mailto link and pop up a more descriptive explanation than the browser error message?


回答1:


Would it be possible to somehow catch attempts to click a mailto link and pop up a more descriptive explanation than the browser error message?

I don't know that you can determine whether a browser supports mailto: links. But as for attaching logic to mailto links, you could cycle through the links on the page, and test their href value. If it begins with "mailto:" you could attach a popup upon clicking it.

var maillinks = document.getElementsByTagName("a");
var (var i = 0; i < maillinks.length; i++) {
  var currentlink = maillinks[i];
  if (currentlink.href.substring(0,7) === "mailto:") {
    alert("Sorry. These aren't allowed.");
    return false;
  }
}

The only real solution I can think to this problem is to host your own contact page, providing a small form that the user can submit.




回答2:


In the general case — I don't think so.

In the specific case of mailto: — no.

To solve the problem you need to describe you need to know if the user has a configured email client, not if the browser supports mailto:. Most browsers support mailto:, and if the user doesn't have a configured client — it still 'works' (by starting the email client and prompting the user to configure it).



来源:https://stackoverflow.com/questions/2274405/is-it-possible-to-test-whether-a-users-browser-os-supports-a-given-type-of-link

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!