Created
November 17, 2011 19:51
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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