forked from MatthewCallis/jquery-age-gate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.agegate.js
126 lines (118 loc) · 3.91 KB
/
jquery.agegate.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/**
* jQuery Age Gate v1.0.0
*
* Simple age verification, "age gate", plugin required by some content providers.
* Supports Cookies when used with https://github.com/carhartl/jquery-cookie
* https://github.com/hark/jquery-age-gate
*
* by Matthew Callis https://github.com/MatthewCallis
* http://www.hark.com
*
* Useage:
* $('#age-restricted').agegate({
* age: 21,
* legal: function(){
* $('#age-restricted').empty().append('<iframe src="..."></iframe>');
* },
* underage: function(){
* alert("Sorry, you must be at least " + this.age + " years old in order to continue.");
* $('#age-restricted').empty();
* }
* });
*
* Released under the GPLv2 license, http://www.gnu.org/licenses/gpl-2.0.html
*/
(function($){
$.fn.agegate = function(options){
var cookie_js = (typeof $.cookie !== "undefined");
return this.each(function(){
var o = $.extend({}, $.fn.agegate.defaults, options);
// If the age_gate cookie is set, use it.
if(cookie_js && $.cookie('age_gate') !== null){
if($.cookie('age_gate') === 'underage'){
o.underage();
return;
}
else if($.cookie('age_gate') === 'legal'){
o.legal();
return;
}
else{
// Invalid Cookie, delete it.
$.cookie('age_gate', null);
}
}
// Build the container and inputs
var gate = $('<div/>').attr({ 'id': o.container_id });
var title = $('<strong/>').text(o.title);
var day = $('<select/>').attr({ 'id': 'agegate-day', 'name': 'agegate-day' }).append(function(){
var days = '';
for(var i = 1; i < 32; i++){
days += '<option value="'+i+'">'+i+'</option>';
}
return days;
});
var month = $('<select/>').attr({ 'id': 'agegate-month', 'name': 'agegate-month' }).append(function(){
var months = '';
for(var i = 0; i < 12; i++){
months += '<option value="'+i+'">' + o.month_names[i] + '</option>';
}
return months;
});
var year = $('<select/>').attr({ 'id': 'agegate-year', 'name': 'agegate-year' }).append(function(){
var years = '';
for(var i = 2011; i > 1900; i--){
years += '<option value="'+i+'">' + i + '</option>';
}
return years;
});
var verify = $('<input/>').attr({ 'type': 'submit', 'id': 'verify', 'name': 'verify', 'value': o.verify_text, 'class': o.verify_class });
verify.bind('click', function(){
var birthday = new Date();
birthday.setFullYear($('#agegate-year').val(), $('#agegate-month').val(), $('#agegate-day').val());
var today = new Date();
today.setFullYear(today.getFullYear() - o.age);
if((today - birthday) < 0){
o.underage();
if(cookie_js){
$.cookie('age_gate', 'underage');
}
}
else{
o.legal();
if(cookie_js){
$.cookie('age_gate', 'legal');
}
}
});
var b = '<br/>';
gate.append(title)
.append(b+b)
.append(o.label_request)
.append(b+b)
.append(o.label_day)
.append(day)
.append(o.label_month)
.append(month)
.append(o.label_year)
.append(year)
.append(b)
.append(verify);
$(this).empty().append(gate);
});
};
$.fn.agegate.defaults = {
age: 18,
container_id: 'age-gate',
verify_text: 'Verify',
verify_class: 'submit',
title: 'Age-Restricted',
label_day: 'Day:',
label_month: 'Month:',
label_year: 'Year:',
label_request: 'Please Enter Your Birthday:',
month_names: ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"],
legal: function(){},
underage: function(){}
};
})(jQuery);