Skip to content

Commit 9c0df9d

Browse files
committed
inital commit
1 parent ba2d696 commit 9c0df9d

File tree

3 files changed

+251
-0
lines changed

3 files changed

+251
-0
lines changed

index.html

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
2+
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
3+
<head>
4+
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
5+
<title>PrimeBox CookieBar Example</title>
6+
<style type="text/css">
7+
* {margin:0; padding:0; outline:0; border:0;}
8+
body {color:#444444; background:#ffffff; font-size:0.8em; font-family:Verdana,Geneva,Arial,Helvetica,sans-serif;}
9+
.container {width:960px; margin:0 auto;}
10+
h1, h2, h3, h4, h5, h6, p, ul, ol {padding:0.35em 0;}
11+
strong {font-weight:bold;}
12+
code {display:block; padding:6px; border:1px solid #bbbbbb; line-height:normal; font-size:1.3em; background:#fafafa; margin:0.35em 0;}
13+
</style>
14+
<link rel="stylesheet" type="text/css" href="jquery.cookiebar.css" />
15+
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
16+
<script type="text/javascript" src="jquery.cookiebar.js"></script>
17+
<script type="text/javascript">
18+
$(document).ready(function(){
19+
$.cookieBar({
20+
});
21+
});
22+
</script>
23+
</head>
24+
<body>
25+
<div class="container">
26+
<h1>jQuery CookieBar example</h1>
27+
<p>This is an example showing the cookie bar's default settings.</p>
28+
<p>Ensure you have jQuery, jQuery CookieBar and jQuery CookieBar's CSS uploaded to your website and linked in the header of your website</p>
29+
<code>
30+
&lt;link rel="stylesheet" type="text/css" href="/css-folder/jquery.cookiebar.css" /&gt;<br />
31+
&lt;script type="text/javascript" src="/scripts-folder/jquery-1.7.1.min.js"&gt;&lt;/script&gt;<br />
32+
&lt;script type="text/javascript" src="/scripts-folder/jquery.cookiebar.js"&gt;&lt;/script&gt;
33+
</code>
34+
<p>Make sure you then include the following line of code within $(document).ready():</p>
35+
<code>
36+
$.cookieBar();
37+
</code>
38+
<p>If you find that the bar appears even after accepting/declining, make sure "forceShow" is set to <strong>false</strong>.</p>
39+
<p>For more information about PrimeBox's CookieBar plugin, go to <a href="http://www.primebox.co.uk/projects/cookie-bar/">www.primebox.co.uk/projects/cookie-bar/</a>.</p>
40+
</div>
41+
</body>
42+
</html>

jquery.cookiebar.css

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#cookie-bar {background:#111111; height:auto; line-height:24px; color:#eeeeee; text-align:center; padding:3px 0;}
2+
#cookie-bar.fixed {position:fixed; top:0; left:0; width:100%;}
3+
#cookie-bar.fixed.bottom {bottom:0; top:auto;}
4+
#cookie-bar p {margin:0; padding:0;}
5+
#cookie-bar a {color:#ffffff; display:inline-block; border-radius:3px; text-decoration:none; padding:0 6px; margin-left:8px;}
6+
#cookie-bar .cb-enable {background:#007700;}
7+
#cookie-bar .cb-enable:hover {background:#009900;}
8+
#cookie-bar .cb-disable {background:#990000;}
9+
#cookie-bar .cb-disable:hover {background:#bb0000;}
10+
#cookie-bar .cb-policy {background:#0033bb;}
11+
#cookie-bar .cb-policy:hover {background:#0055dd;}

jquery.cookiebar.js

+198
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
1+
/*
2+
* Copyright (C) 2012 PrimeBox ([email protected])
3+
*
4+
* This work is licensed under the Creative Commons
5+
* Attribution 3.0 Unported License. To view a copy
6+
* of this license, visit
7+
* http://creativecommons.org/licenses/by/3.0/.
8+
*
9+
* Documentation available at:
10+
* http://www.primebox.co.uk/projects/cookie-bar/
11+
*
12+
* When using this software you use it at your own risk. We hold
13+
* no responsibility for any damage caused by using this plugin
14+
* or the documentation provided.
15+
*/
16+
(function($){
17+
$.cookieBar = function(options,val){
18+
if(options=='cookies'){
19+
var doReturn = 'cookies';
20+
}else if(options=='set'){
21+
var doReturn = 'set';
22+
}else{
23+
var doReturn = false;
24+
}
25+
var defaults = {
26+
message: 'We use cookies to track usage and preferences.', //Message displayed on bar
27+
acceptButton: true, //Set to true to show accept/enable button
28+
acceptText: 'I Understand', //Text on accept/enable button
29+
acceptFunction: function(cookieValue){if(cookieValue!='enabled' && cookieValue!='accepted') window.location = window.location.href;}, //Function to run after accept
30+
declineButton: false, //Set to true to show decline/disable button
31+
declineText: 'Disable Cookies', //Text on decline/disable button
32+
declineFunction: function(cookieValue){if(cookieValue=='enabled' || cookieValue=='accepted') window.location = window.location.href;}, //Function to run after decline
33+
policyButton: false, //Set to true to show Privacy Policy button
34+
policyText: 'Privacy Policy', //Text on Privacy Policy button
35+
policyURL: '/privacy-policy/', //URL of Privacy Policy
36+
autoEnable: true, //Set to true for cookies to be accepted automatically. Banner still shows
37+
acceptOnContinue: false, //Set to true to accept cookies when visitor moves to another page
38+
acceptOnScroll: false, //Set to true to accept cookies when visitor scrolls X pixels up or down
39+
acceptAnyClick: false, //Set to true to accept cookies when visitor clicks anywhere on the page
40+
expireDays: 365, //Number of days for cookieBar cookie to be stored for
41+
renewOnVisit: false, //Renew the cookie upon revisit to website
42+
forceShow: false, //Force cookieBar to show regardless of user cookie preference
43+
effect: 'slide', //Options: slide, fade, hide
44+
element: 'body', //Element to append/prepend cookieBar to. Remember "." for class or "#" for id.
45+
append: false, //Set to true for cookieBar HTML to be placed at base of website. Actual position may change according to CSS
46+
fixed: false, //Set to true to add the class "fixed" to the cookie bar. Default CSS should fix the position
47+
bottom: false, //Force CSS when fixed, so bar appears at bottom of website
48+
zindex: '', //Can be set in CSS, although some may prefer to set here
49+
domain: String(window.location.hostname), //Location of privacy policy
50+
referrer: String(document.referrer) //Where visitor has come from
51+
};
52+
var options = $.extend(defaults,options);
53+
54+
//Sets expiration date for cookie
55+
var expireDate = new Date();
56+
expireDate.setTime(expireDate.getTime()+(options.expireDays*86400000));
57+
expireDate = expireDate.toGMTString();
58+
59+
var cookieEntry = 'cb-enabled={value}; expires='+expireDate+'; path=/';
60+
61+
//Retrieves current cookie preference
62+
var i,cookieValue='',aCookie,aCookies=document.cookie.split('; ');
63+
for (i=0;i<aCookies.length;i++){
64+
aCookie = aCookies[i].split('=');
65+
if(aCookie[0]=='cb-enabled'){
66+
cookieValue = aCookie[1];
67+
}
68+
}
69+
//Sets up default cookie preference if not already set
70+
if(cookieValue=='' && doReturn!='cookies' && options.autoEnable){
71+
cookieValue = 'enabled';
72+
document.cookie = cookieEntry.replace('{value}','enabled');
73+
}else if((cookieValue=='accepted' || cookieValue=='declined') && doReturn!='cookies' && options.renewOnVisit){
74+
document.cookie = cookieEntry.replace('{value}',cookieValue);
75+
}
76+
if(options.acceptOnContinue){
77+
if(options.referrer.indexOf(options.domain)>=0 && String(window.location.href).indexOf(options.policyURL)==-1 && doReturn!='cookies' && doReturn!='set' && cookieValue!='accepted' && cookieValue!='declined'){
78+
doReturn = 'set';
79+
val = 'accepted';
80+
}
81+
}
82+
if(doReturn=='cookies'){
83+
//Returns true if cookies are enabled, false otherwise
84+
if(cookieValue=='enabled' || cookieValue=='accepted'){
85+
return true;
86+
}else{
87+
return false;
88+
}
89+
}else if(doReturn=='set' && (val=='accepted' || val=='declined')){
90+
//Sets value of cookie to 'accepted' or 'declined'
91+
document.cookie = cookieEntry.replace('{value}',val);
92+
if(val=='accepted'){
93+
return true;
94+
}else{
95+
return false;
96+
}
97+
}else{
98+
//Sets up enable/accept button if required
99+
var message = options.message.replace('{policy_url}',options.policyURL);
100+
101+
if(options.acceptButton){
102+
var acceptButton = '<a href="" class="cb-enable">'+options.acceptText+'</a>';
103+
}else{
104+
var acceptButton = '';
105+
}
106+
//Sets up disable/decline button if required
107+
if(options.declineButton){
108+
var declineButton = '<a href="" class="cb-disable">'+options.declineText+'</a>';
109+
}else{
110+
var declineButton = '';
111+
}
112+
//Sets up privacy policy button if required
113+
if(options.policyButton){
114+
var policyButton = '<a href="'+options.policyURL+'" class="cb-policy">'+options.policyText+'</a>';
115+
}else{
116+
var policyButton = '';
117+
}
118+
//Whether to add "fixed" class to cookie bar
119+
if(options.fixed){
120+
if(options.bottom){
121+
var fixed = ' class="fixed bottom"';
122+
}else{
123+
var fixed = ' class="fixed"';
124+
}
125+
}else{
126+
var fixed = '';
127+
}
128+
if(options.zindex!=''){
129+
var zindex = ' style="z-index:'+options.zindex+';"';
130+
}else{
131+
var zindex = '';
132+
}
133+
134+
//Displays the cookie bar if arguments met
135+
if(options.forceShow || cookieValue=='enabled' || cookieValue==''){
136+
if(options.append){
137+
$(options.element).append('<div id="cookie-bar"'+fixed+zindex+'><p>'+message+acceptButton+declineButton+policyButton+'</p></div>');
138+
}else{
139+
$(options.element).prepend('<div id="cookie-bar"'+fixed+zindex+'><p>'+message+acceptButton+declineButton+policyButton+'</p></div>');
140+
}
141+
}
142+
143+
var removeBar = function(func){
144+
if(options.acceptOnScroll) $(document).off('scroll');
145+
if(typeof(func)==='function') func(cookieValue);
146+
if(options.effect=='slide'){
147+
$('#cookie-bar').slideUp(300,function(){$('#cookie-bar').remove();});
148+
}else if(options.effect=='fade'){
149+
$('#cookie-bar').fadeOut(300,function(){$('#cookie-bar').remove();});
150+
}else{
151+
$('#cookie-bar').hide(0,function(){$('#cookie-bar').remove();});
152+
}
153+
$(document).unbind('click',anyClick);
154+
};
155+
var cookieAccept = function(){
156+
document.cookie = cookieEntry.replace('{value}','accepted');
157+
removeBar(options.acceptFunction);
158+
};
159+
var cookieDecline = function(){
160+
var deleteDate = new Date();
161+
deleteDate.setTime(deleteDate.getTime()-(864000000));
162+
deleteDate = deleteDate.toGMTString();
163+
aCookies=document.cookie.split('; ');
164+
for (i=0;i<aCookies.length;i++){
165+
aCookie = aCookies[i].split('=');
166+
if(aCookie[0].indexOf('_')>=0){
167+
document.cookie = aCookie[0]+'=0; expires='+deleteDate+'; domain='+options.domain.replace('www','')+'; path=/';
168+
}else{
169+
document.cookie = aCookie[0]+'=0; expires='+deleteDate+'; path=/';
170+
}
171+
}
172+
document.cookie = cookieEntry.replace('{value}','declined');
173+
removeBar(options.declineFunction);
174+
};
175+
var anyClick = function(e){
176+
if(!$(e.target).hasClass('cb-policy')) cookieAccept();
177+
};
178+
179+
$('#cookie-bar .cb-enable').click(function(){cookieAccept();return false;});
180+
$('#cookie-bar .cb-disable').click(function(){cookieDecline();return false;});
181+
if(options.acceptOnScroll){
182+
var scrollStart = $(document).scrollTop(),scrollNew,scrollDiff;
183+
$(document).on('scroll',function(){
184+
scrollNew = $(document).scrollTop();
185+
if(scrollNew>scrollStart){
186+
scrollDiff = scrollNew - scrollStart;
187+
}else{
188+
scrollDiff = scrollStart - scrollNew;
189+
}
190+
if(scrollDiff>=Math.round(options.acceptOnScroll)) cookieAccept();
191+
});
192+
}
193+
if(options.acceptAnyClick){
194+
$(document).bind('click',anyClick);
195+
}
196+
}
197+
};
198+
})(jQuery);

0 commit comments

Comments
 (0)