Skip to content

Instantly share code, notes, and snippets.

@justinobney
Created November 17, 2011 19:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save justinobney/1374275 to your computer and use it in GitHub Desktop.
Save justinobney/1374275 to your computer and use it in GitHub Desktop.
Converts telephone numbers on a page to "tel:#" links for opening via a pc-phone
(function($) {
$.fn.Telephonify = function() {
var patt = /\(?[2-9]\d{2}\)?[- ]?\d{3}[- ]?\d{4}/gi;
return this.each(function() {
var el = $(this);
var html = el.html();
var result = html.match(patt);
function eliminateDuplicates(arr) {
var i,
len = arr.length,
out = [],
obj = {};
for (i = 0; i < len; i++) {
obj[arr[i]] = 0;
}
for (i in obj) {
out.push(i);
}
return out;
}
if (result) {
var distinctArray = eliminateDuplicates(result);
var compiled = html;
jQuery.each(distinctArray, function(i, val) {
var phoneNum = val.replace(/[^0-9]/g, '');
compiled = html.replace(val, '<a href=tel:' + phoneNum + '>' + val + '<\/a>');
html = compiled;
});
el.html(compiled);
}
});
};
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment