-
Notifications
You must be signed in to change notification settings - Fork 14
/
jquery.html5support.js
94 lines (81 loc) · 3.24 KB
/
jquery.html5support.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/*
* http://github.com/amiel/html5support
* Amiel Martin
* 2010-01-26
*
* Support certain HTML 5 attributes with javascript, but only if the browser doesn't already support them.
*/
var HTML5Support = (function($){
var // private members
// if no value is specified, find placeholder text in this html attribute
placeholder_attribute = 'placeholder',
// give the input field this class when the placeholder text is used
placeholder_klass = placeholder_attribute,
// this will become our HTML5Support object
h5 = {};
// public functions
$.extend(h5, {
supports_attribute: function(attribute, type) { // should we memoize this?
return attribute in document.createElement(type || 'input');
}
});
// private functions
function tabularosa() {
var self = $(this),
// I've added three unprintable invisible characters to the end here.
// That indicates without a doubt that the value in the box was put
// there as a placeholder and not by the user. This is great for
// situations when the placeholder might be "USA" and the user enters
// the string "USA", and it disappears and gets a placeholder class.
// I tried fixing it the right way, but that didn't seem to work across
// refreshes in firefox.
value = self.attr(placeholder_attribute) + "\u00A0\u00A0\u00A0",
set_value = function() {
if ($.trim(self.val()) == '' || self.val() == value)
self.val(value).addClass(placeholder_klass);
},
clear_value = function() {
if (self.val() == value) {
self.val('');
}
self.removeClass(placeholder_klass);
};
self.focus(clear_value).blur(set_value).blur();
}
// this one is sort of hacky
function password_tabularosa() {
var self = $(this),
value = self.attr(placeholder_attribute),
placeholder_input = $('<input type="text">').val(value).
addClass(placeholder_klass).addClass(self.attr('class')).
css('display', 'none');
set_value = function() {
if ($.trim(self.val()) == '') {
placeholder_input.show();
self.hide();
}
},
clear_value = function() {
placeholder_input.hide();
self.show().focus();
};
self.after(placeholder_input);
placeholder_input.focus(clear_value);
self.blur(set_value).blur();
}
// jquery plugins
$.fn.placeholder = function(value) {
if (h5.supports_attribute('placeholder')) return this;
return this.each(function() {
($(this).attr('type') == 'password') ? password_tabularosa.apply(this) : tabularosa.apply(this);
});
};
$.fn.autofocus = function() {
if (h5.supports_attribute('autofocus')) return this;
return this.focus();
};
$.autofocus = function() { $('[autofocus]:visible').autofocus(); };
$.placeholder = function() { $('['+placeholder_attribute+']').placeholder(); };
$.html5support = function() { $.autofocus(); $.placeholder(); };
return h5;
})(jQuery);