How can I add a phone number to a website that is clickable but hides the link when I\'m browsing on a website that doesn\'t support touch.
I could use Modernizr to set
This way works without adding any more CSS.
Try replacing the a
tag with something else like a span
tag, but only for mobile browsers of course. Benefits are that you are not cloaking this a
with CSS preventing default action while keeping it still as a link. It won't be a
anymore, so you won't have to worry.
I've created an example to here. Bold phone there works this way, see code below.
I took piece of code from one of the respondent to define if browser is mobile. And you have to mark these links with class="tel"
or find a way to determine if the href
has "tel:" in it. JQuery is also required.
// define if browser is mobile, usually I use Anthony's Hand mobile detection library, but here is simple detection for clarity
var probablyPhone = ((/iphone|android|ie|blackberry|fennec/).test(navigator.userAgent.toLowerCase()) && 'ontouchstart' in document.documentElement);
if ( !probablyPhone ) {
// find all the A with "tel:" in it, by class name
$('a.tel').each(function(){
var span = document.createElement('span');
// SPAN inherits properties of A, you can also add STYLE or TITLE or whatever
span.innerHTML = this.innerHTML;
span.className = this.className;
// replace A with SPAN
this.parentNode.insertBefore(span, this);
this.parentNode.removeChild(this);
});
}