-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.mailto.js
63 lines (59 loc) · 2.1 KB
/
jquery.mailto.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/**
* jQuery MailTo
* Simple jQuery plugin to hide email addresses from spambots
*
* Depends:
* jquery.js 1.7+
*
* Demo:
* http://projects.sergiodinislopes.pt/mailto/example/
*
* Github:
* https://github.com/sergiodlopes/mailto/
*/
(function($) {
$.fn.mailto = function(options) {
return this.each(function() {
var $elem = $(this);
var _options = $.extend({
text: false, // By default link text is the email address
host: window.location.hostname.replace('www.',''), // Default host
account: false, // Account
prepend: false // Prepend email address
}, options, $elem.data());
// Get email address
var $emailAddress = function() {
var $email = '';
// Email account name its in the attribute
if(_options.account) {
return _options.account + '@' + _options.host;
} else {
var $text = $elem.text();
$elem.contents().filter(function() {
return this.nodeType != 1;
}).remove();
return $text.replace(' at ', '@').split(' dot ').join('.');
}
return $email;
}
var $email = $emailAddress();
if(!_options.text){
_options.text = $email;
}
// Prepend email address to element?
if(_options.prepend){
$elem.prepend(_options.text + ' ');
} else {
$elem.append(' ' + _options.text);
}
// If is <a>
if($elem.is('a')){
var $mailto = 'mailto:' + $email;
if(_options.subject){
$mailto += '?subject=' + encodeURIComponent( _options.subject );
}
$elem.attr('href', $mailto);
}
});
};
})(jQuery);