简体   繁体   English

如何标记电话号码?

[英]How to mark-up phone numbers?

I want to mark up a phone number as callable link in an HTML document.我想在 HTML 文档中将电话号码标记为可调用链接。 I have read the microformats approach , and I know, that the tel: scheme would be standard, but is quite literally nowhere implemented.我已经阅读了微格式方法,并且我知道tel:方案将是标准的,但实际上并没有实现。

Skype defines, as far as I know, skype: and callto: , the latter having gained some popularity.据我所知,Skype 定义了skype:callto: ,后者已经流行起来。 I assume, that other companies have either other schemes or jump on the callto: train.我假设,其他公司要么有其他计划,要么跳上callto:火车。

What would be a best practice to mark-up a phone number, so that as many people as possible with VoIP software can just click on a link to get a call?标记电话号码的最佳做法是什么,以便尽可能多的使用 VoIP 软件的人只需单击链接即可接听电话?

Bonus question: Does anyone know about complications with emergency numbers such as 911 in US or 110 in Germany?奖励问题:有谁知道紧急号码的并发症,例如美国的 911 或德国的 110?

Cheers,干杯,

Update: Microsoft NetMeeting takes callto: schemes under WinXP.更新: Microsoft NetMeeting 在 WinXP 下采用callto:方案。 This question suggests, that Microsoft Office Communicator will handle tel: schemes but not callto: ones. 这个问题表明,Microsoft Office Communicator 将处理tel:方案但不处理callto:方案。 Great, Redmond!太好了,雷德蒙德!

Update 2: Two and a half years later now.更新 2:两年半后的现在。 It seems to boil down to what you want to do with the number.它似乎归结为你想用这个数字做什么。 In mobile context, tel: is the way to go.在移动上下文中, tel:是必经之路。 Targeting desktops it's up to you, if you think your users are more Skype people ( callto: ) or will more likely have something like Google Voice ( tel: ) installed.如果您认为您的用户更多的是 Skype 用户 ( callto:或更有可能安装 Google Voice ( tel: :) 之类的东西,那么针对台式机这取决于您。 My personal opinion is, when in doubt use tel: (in line with @Sidnicious' answer).我个人的意见是,如有疑问,请使用tel:与@Sidnicious 的回答一致)。

Update 3: User @rybo111 noted, that Skype in Chrome has meanwhile jumped on the tel: bandwagon.更新 3:用户 @rybo111 指出,Chrome 中的 Skype 同时加入了tel:潮流。 I cannot verify this, because no machine with both at hand, but if it's true, it means we have finally a winner here: tel:我无法证实这一点,因为手边没有机器,但如果这是真的,那就意味着我们终于在这里有一个赢家: tel:

The tel: scheme was used in the late 1990s and documented in early 2000 with RFC 2806 (which was obsoleted by the more-thorough RFC 3966 in 2004) and continues to be improved . tel:方案在 1990 年代后期使用,并在 2000 年初与RFC 2806一起记录(在 2004 年被更全面的RFC 3966淘汰)并继续得到改进 Supporting tel: on the iPhone was not an arbitrary decision.支持tel:在 iPhone 上并不是一个武断的决定。

callto: , while supported by Skype, is not a standard and should be avoided unless specifically targeting Skype users. callto:虽然受 Skype 支持,但不是标准,除非专门针对 Skype 用户,否则应避免使用。

Me?我? I'd just start including properly-formed tel: URIs on your pages (without sniffing the user agent) and wait for the rest of the world's phones to catch up:).我只是开始在您的页面上包含格式正确的tel: URI(不嗅探用户代理),然后等待世界其他地方的电话赶上:)。

Example :示例

 <a href="tel:+18475555555">1-847-555-5555</a>

My test results:我的测试结果:

callto:拨电至:

  • Nokia Browser: nothing happens诺基亚浏览器:没有任何反应
  • Google Chrome: asks to run skype to call the number谷歌浏览器:要求运行 skype 来拨打号码
  • Firefox: asks to choose a program to call the number Firefox:要求选择一个程序来呼叫该号码
  • IE: asks to run skype to call the number IE:要求运行 skype 来呼叫该号码

tel:电话:

  • Nokia Browser: working诺基亚浏览器:工作
  • Google Chrome: nothing happens谷歌浏览器:没有任何反应
  • Firefox: "Firefox doesnt know how to open this url" Firefox:“Firefox 不知道如何打开此 URL”
  • IE: could not find url IE:找不到网址

The best bet is to start off with tel: which works on all mobiles最好的办法是从 tel: 开始,它适用于所有手机

Then put in this code, which will only run when on a desktop, and only when a link is clicked.然后放入这段代码,它只会在桌面上运行,并且只有在单击链接时才会运行。

I'm using http://detectmobilebrowsers.com/ to detect mobile browsers, you can use whatever method you prefer我正在使用http://detectmobilebrowsers.com/来检测移动浏览器,您可以使用您喜欢的任何方法

if (!jQuery.browser.mobile) {
    jQuery('body').on('click', 'a[href^="tel:"]', function() {
            jQuery(this).attr('href', 
                jQuery(this).attr('href').replace(/^tel:/, 'callto:'));
    });
}

So basically you cover all your bases.所以基本上你涵盖了所有的基础。

tel: works on all phones to open the dialer with the number电话:适用于所有电话以使用号码打开拨号器

callto: works on your computer to connect to skype from firefox, chrome callto:在你的电脑上工作,从 firefox、chrome 连接到 skype

As one would expect, WebKit's support of tel: extends to the Android mobile browser as well - FYI正如人们所预料的那样,WebKit 对tel:的支持也扩展到了 Android 移动浏览器——仅供参考

I keep this answer for "historic" purpose but don't recommend it anymore.我出于“历史”目的保留此答案,但不再推荐它。 See @Sidnicious' answer above and my Update 2.请参阅上面的@Sidnicious 的回答和我的更新 2。

Since it looks like a draw between callto and tel guys, I want to throw in a possible solution in the hope, that your comments will bring me back on the way of light;-)由于它看起来像是 callto 和 tel 家伙之间的平局,我想提出一个可能的解决方案,希望您的评论能让我回到光明的道路上;-)

Using callto: , since most desktop clients will handle it:使用callto: ,因为大多数桌面客户端都会处理它:

<a href="callto:0123456789">call me</a>

Then, if the client is an iPhone, replace the links:然后,如果客户端是 iPhone,则替换链接:

window.onload = function () {
  if (navigator.userAgent.match (/iPhone/i)) {
    var a = document.getElementsByTagName ("a");
    for (var i = 0; i < a.length; i++) {
      if (a[i].getAttribute ('href').search (/callto:/i) === 0) {
        a[i].setAttribute ('href', a[i].getAttribute ('href').replace (/^callto:/, "tel:"));
      }
    }
  }
};

Any objections against this solution?对这个解决方案有异议吗? Should I preferably start from tel: ?我最好从tel:开始吗?

Mobile Safari (iPhone & iPod Touch) use the tel: scheme. Mobile Safari(iPhone 和 iPod Touch)使用tel:方案。

How do I dial a phone number from a webpage on iPhone? 如何从 iPhone 上的网页拨打电话号码?

RFC3966 defines the IETF standard URI for telephone numbers, that is the 'tel:' URI. RFC3966定义了电话号码的 IETF 标准 URI,即“tel:”URI。 That's the standard.这就是标准。 There's no similar standard that specifies 'callto:', that's a particular convention for Skype on platforms where is allows registering a URI handler to support it.没有类似的标准指定“callto:”,这是 Skype 在允许注册 URI 处理程序以支持它的平台上的特定约定。

this worked for me:这对我有用:

1.make a standards compliant link: 1.制作符合标准的链接:

        <a href="tel:1500100900">

2.replace it when mobile browser is not detected, for skype: 2.未检测到移动浏览器时替换它,对于skype:

$("a.phone")
    .each(function()
{ 
  this.href = this.href.replace(/^tel/, 
     "callto");
});

Selecting link to replace via class seems more efficient.选择通过类替换的链接似乎更有效率。 Of course it works only on anchors with .phone class.当然,它仅适用于具有.phone类的锚点。

I have put it in function if(.isMobile() ) {... so it triggers only when detects desktop browser.我将它放在函数if(.isMobile() ) {... ,因此它仅在检测到桌面浏览器时触发。 But this one is problably obsolete...但是这个可能已经过时了......

function isMobile() {
    return (
        ( navigator.userAgent.indexOf( "iPhone" ) > -1 ) ||
        ( navigator.userAgent.indexOf( "iPod" ) > -1 ) ||
        ( navigator.userAgent.indexOf( "iPad" ) > -1 ) ||
        ( navigator.userAgent.indexOf( "Android" ) > -1 ) ||
        ( navigator.userAgent.indexOf( "webOS" ) > -1 )
    );
}

I used tel: for my project.我在我的项目中使用了tel:

It worked in Chrome, Firefox, IE9&8, Chrome mobile and the mobile Browser on my Sony Ericsson smartphone.它适用于 Chrome、Firefox、IE9&8、Chrome 移动版和我的索尼爱立信智能手机上的移动浏览器。

But callto: did not work in the mobile Browsers.但是callto:在移动浏览器中不起作用。

I would use tel: (as recommended).我会使用tel:按照推荐)。 But to have a better fallback/not display error pages I would use something like this (using jquery):但是为了有更好的后备/不显示错误页面,我会使用这样的东西(使用 jquery):

// enhance tel-links
$("a[href^='tel:']").each(function() {
    var target = "call-" + this.href.replace(/[^a-z0-9]*/gi, "");
    var link = this;

    // load in iframe to supress potential errors when protocol is not available
    $("body").append("<iframe name=\"" + target + "\" style=\"display: none\"></iframe>");
    link.target = target;

    // replace tel with callto on desktop browsers for skype fallback
    if (!navigator.userAgent.match(/(mobile)/gi)) {
        link.href = link.href.replace(/^tel:/, "callto:");
    }
});

The assumption is, that mobile browsers that have a mobile stamp in the userAgent-string have support for the tel: protocol.假设是,在 userAgent 字符串中带有移动标记的移动浏览器支持tel:协议。 For the rest we replace the link with the callto: protocol to have a fallback to Skype where available.对于其余部分,我们将链接替换为callto:协议,以便在可用时回退到 Skype。

To suppress error-pages for the unsupported protocol(s), the link is targeted to a new hidden iframe.为了抑制不支持的协议的错误页面,链接指向一个新的隐藏 iframe。

Unfortunately it does not seem to be possible to check, if the url has been loaded successfully in the iframe.不幸的是,似乎无法检查 url 是否已成功加载到 iframe 中。 It's seems that no error events are fired.似乎没有触发任何错误事件。

Since callto: is per default supported by skype (set up in Skype settings), and others do also support it, I would recommend using callto: rather than skype: .由于callto:是 skype 默认支持的(在 Skype 设置中设置),其他人也支持它,我建议使用callto:而不是skype:

Although Apple recommends tel: in their docs for Mobile Safari, currently (iOS 4.3) it accepts callto: just the same.尽管 Apple 在他们的 Mobile Safari 文档中推荐tel: :,但目前(iOS 4.3)它接受callto:是一样的。 So I recommend using callto: on a generic web site as it works with both Skype and iPhone and I expect it will work on Android phones, too.所以我建议在通用网站上使用callto:因为它适用于 Skype 和 iPhone,我希望它也适用于 Android 手机。

Update (June 2013)更新(2013 年 6 月)

This is still a matter of deciding what you want your web page to offer.这仍然是决定您希望网页提供什么的问题。 On my websites I provide both tel: and callto: links (the latter labeled as being for Skype) since Desktop browsers on Mac don't do anything with tel: links while mobile Android doesn't do anything with callto: links.在我的网站上,我提供了tel:callto:链接(后者标记为用于 Skype),因为 Mac 上的桌面浏览器不会对tel:链接做任何事情,而移动 Android 不会对callto:链接做任何事情。 Even Google Chrome with the Google Talk plugin does not respond to tel: links.即使是带有 Google Talk 插件的谷歌浏览器也不会响应tel:链接。 Still, I prefer offering both links on the desktop in case someone has gone to the trouble of getting tel: links to work on their computer.不过,我更喜欢在桌面上提供这两个链接,以防有人不厌其烦地在他们的计算机上获取tel:链接。

If the site design dictated that I only provide one link, I'd use a tel: link that I would try to change to callto: on desktop browsers.如果网站设计要求我只提供一个链接,我会使用一个tel:链接,我会尝试在桌面浏览器上将其更改为callto:

Using jQuery, replace all US telephone numbers on the page with the appropriate callto: or tel: schemes.使用 jQuery,将页面上的所有美国电话号码替换为适当的callto:tel:方案。

// create a hidden iframe to receive failed schemes
$('body').append('<iframe name="blackhole" style="display:none"></iframe>');

// decide which scheme to use
var scheme = (navigator.userAgent.match(/mobile/gi) ? 'tel:' : 'callto:');

// replace all on the page
$('article').each(function (i, article) {
    findAndReplaceDOMText(article, {
        find:/\b(\d\d\d-\d\d\d-\d\d\d\d)\b/g,
        replace:function (portion) {
            var a = document.createElement('a');
            a.className = 'telephone';
            a.href = scheme + portion.text.replace(/\D/g, '');
            a.textContent = portion.text;
            a.target = 'blackhole';
            return a;
        }
    });
});

Thanks to @jonas_jonas for the idea.感谢@jonas_jonas 的想法。 Requires the excellent findAndReplaceDOMText function .需要优秀的findAndReplaceDOMText 函数

I use the normal <a href="tel:+123456">12 34 56</a> markup and make those links non-clickable for desktop users via pointer-events: none;我使用普通的<a href="tel:+123456">12 34 56</a>标记,并通过pointer-events: none;使桌面用户无法点击这些链接。

a[href^="tel:"] {
    text-decoration: none;
}
.no-touch a[href^="tel:"] {
    pointer-events: none;
    cursor: text;
}

for browsers that don't support pointer-events (IE < 11), the click can be prevented with JavaScript (example relies on Modernizr and jQuery):对于不支持指针事件的浏览器(IE < 11),可以使用 JavaScript 阻止点击(示例依赖于 Modernizr 和 jQuery):

if(!Modernizr.touch) {
    $(document).on('click', '[href^="tel:"]', function(e) {
        e.preventDefault();
        return false;
    });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM