Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make an <a href> only active on mobile devices?

I want to add this code to make the contact details on my website automatically jump into a mobile dialpad/address book when clicked:

<a href="tel:12345555555">Call Us Now</a>

How can I make it appear as plain text for non-telephone devices (computers, tablets, laptops) so that it doesn't return a 404 error and cannot be clicked?

Happy to use any combination of HTML/CSS/JS to achieve this.

like image 246
Dom Avatar asked Nov 16 '12 01:11

Dom


People also ask

What is a Telephone link?

What is a Tel Link? Tel link is a telephone link used on websites. Simply said, it makes your phone number clickable. Therefore, whenever the customer wants to contact you, he/she can click on the link and connect with your customer support right away.

How do you activate a link in HTML?

To make a hyperlink in an HTML page, use the <a> and </a> tags, which are the tags used to define the links. The <a> tag indicates where the hyperlink starts and the </a> tag indicates where it ends. Whatever text gets added inside these tags, will work as a hyperlink. Add the URL for the link in the <a href=” ”>.


2 Answers

Don't use a hyperlink. Use microdata. Mobile phones will recognize that it is a phone number and allow it to be clicked but it won't affect normal web browsers.

<div class="vcard">
    <div class="tel">
        12345555555
    </div>
</div>
like image 87
John Conde Avatar answered Sep 27 '22 15:09

John Conde


This might not directly answer the question, but I'd challenge your assumption that you need to hide call links on other devices.

Why?

  • Non-phone devices can still make calls. For example, a PC with a VoIP client can handle tel: links.
  • Tablets (iPad and Android) handle tel: links by allowing the user to add the number to their contacts, which would undoubtedly be synced to their phone – a nice convenience for your users.
  • Relying on automatic format detection is a hack.

So just leave it as a regular link. Maybe make it obvious by linking the phone number, so that someone on a desktop with no telephony software will understand that nothing will happen when they click it.

Call Us Now at <a href="tel:12345555555">(234) 555-5555</a>

Also, remember that a tel: link won't result in a 404 error since a HTTP request is never generated. On my machine with no tel: handler, Chrome simply does nothing, IE9 says "Some content or files on this webpage require a program that you don't have installed." (reasonable), and Firefox says "Firefox doesn't know how to open this address, because the protocol (tel) isn't associated with any program." (also reasonable).

When I was faced with this problem, I decided that the benefits of just leaving in tel: links outweighed any downsides or messy alternatives.

like image 29
josh3736 Avatar answered Sep 27 '22 16:09

josh3736