Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

252
LINES

< > BotCompany Repo | #1021514 // cookie-warn.js

Document

1  
/**
2  
 * EXAMPLE 1:
3  
 * 
4  
 *    <html lang="en" ... >
5  
 *    ...
6  
 *    <script
7  
 *        id="cookieWarn"
8  
 *        data-lang-en="{
9  
 *              'text': 'Our website uses cookies.',
10  
 *              'more_text': 'Click here for more information',
11  
 *              'more_link': 'http://ec.europa.eu/ipg/basics/legal/cookies/index_en.htm',
12  
 *              'accept_text': 'I accept',
13  
 *              'reject_text': 'I reject',
14  
 *              'reject_info': 'You can disable unwanted cookies by using this program',
15  
 *              'reject_link': 'https://www.ghostery.com/'
16  
 *           },
17  
 *        }"
18  
 *        data-expire="365" (optional, default 365 day)
19  
 *        data-domain="*.domain.tld" (cookie domain, optional)
20  
 *        data-path="/" (cookie path, optional)
21  
 *        data-secure="true" (cookie secure, true / false, optional)
22  
 *        data-delay="750" (optional, default 500)
23  
 *        data-class="customCookieWarning" (optional)
24  
 *        data-style="#cookieWarnBox a { color: #ff0000; }" (optional)
25  
 *        type="text/javascript"
26  
 *        src="../cookie-warn.min.js">
27  
 *    </script>
28  
 *
29  
 * EXAMPLE 2:
30  
 *    
31  
 *    <html lang="en" ... >
32  
 *    ...
33  
 *    <script type="text/javascript" src="../cookie-warn.min.js"></script>
34  
 * 
35  
 *    <div
36  
 *        id="cookieWarn"
37  
 *        data-lang-en="{
38  
 *              'text':'Our website uses cookies.',
39  
 *              'accept_text':'I accept',
40  
 *              'more_text':'Click here for more information',
41  
 *              'more_link':'http://ec.europa.eu/ipg/basics/legal/cookies/index_en.htm'
42  
 *        }"
43  
 *        data-lang-hu="{
44  
 *              'text':'Weboldalunk sütiket használ.',
45  
 *              'accept_text':'Elfogadom',
46  
 *              'more_text':'Kattints ide a bÅ‘vebb információért',
47  
 *              'more_link':'http://ec.europa.eu/ipg/basics/legal/cookies/index_en.htm'
48  
 *        }">
49  
 *    </div>
50  
 *
51  
 */
52  
53  
(function (fn) {
54  
55  
    "use strict";
56  
57  
    // set or get cookie
58  
    var cookie = function (name, value, days, path, domain, secure) {
59  
60  
        if (value === undefined) {
61  
62  
            var i, x, y, cookies = document.cookie.split(";");
63  
64  
            for (i = 0; i < cookies.length; i++) {
65  
66  
                x = cookies[i].substr(0, cookies[i].indexOf("="));
67  
                y = cookies[i].substr(cookies[i].indexOf("=") + 1);
68  
                x = x.replace(/^\s+|\s+$/g, "");
69  
70  
                if (x == name) {
71  
                    return y;
72  
                }
73  
74  
            }
75  
76  
        } else {
77  
78  
            days = days ? days : 365;
79  
            var expire = new Date();
80  
            expire.setDate(expire.getDate() + days);
81  
            var values = [];
82  
83  
            if (days !== undefined && days !== null) {
84  
                values.push("expires=" + expire.toGMTString());
85  
            }
86  
87  
            if (path !== undefined && path !== null) {
88  
                values.push("path=" + path);
89  
            }
90  
91  
            if (domain !== undefined && domain !== null) {
92  
                values.push("domain=" + domain);
93  
            }
94  
95  
            if (secure !== undefined && secure !== null && secure) {
96  
                values.push("secure");
97  
            }
98  
99  
            if (values.length > 0) {
100  
                value = value + "; " + values.join("; ");
101  
            }
102  
103  
            document.cookie = escape(name) + "=" + value;
104  
105  
        }
106  
107  
    };
108  
109  
    // if cookie available then return
110  
    if (cookie(fn)) {
111  
        return;
112  
    }
113  
114  
    // warning box close function
115  
    window[fn] = {
116  
117  
        close: function (expire, path, domain, secure) {
118  
119  
            // set the cookie
120  
            cookie(fn, true, expire, path, domain, secure);
121  
122  
            // remove warning box
123  
            var wbox = document.getElementById(fn + 'Box');
124  
            wbox.className = wbox.className + ' closed';
125  
126  
        },
127  
128  
        reject: function () {
129  
130  
            // show reject information
131  
            var wbox = document.getElementById(fn + 'Box');
132  
            wbox.className = wbox.className + ' reject';
133  
134  
        }
135  
136  
    };
137  
138  
139  
    var warn = function () {
140  
141  
        // get parameters
142  
        var tag = document.getElementById('cookieWarn');
143  
144  
        if (!tag) {
145  
            console.error('cookieWarn element not found by id');
146  
            return;
147  
        }
148  
149  
        var lang = document.documentElement.lang;
150  
        var data = JSON.parse(tag.getAttribute('data-lang-' + lang).replace(/'/g, '"'));
151  
152  
        if (!data) {
153  
            return;
154  
        }
155  
156  
        var delay = parseInt(tag.getAttribute('data-delay'));
157  
        var domain = tag.getAttribute('data-domain');
158  
        var path = tag.getAttribute('data-path');
159  
        var secure = tag.getAttribute('data-secure');
160  
        var expire = parseInt(tag.getAttribute('data-expire'));
161  
        var style = tag.getAttribute('data-style');
162  
        var classes = tag.getAttribute('data-class');
163  
164  
        var bootstrap = (window.jQuery && typeof $().modal == 'function');
165  
166  
        var css = {
167  
168  
            style: [
169  
                '#' + fn + 'Box {transition:all 0.4s ease-in-out;position:fixed;z-index:999999;bottom:-20px;left:0;right:0;opacity:0;text-align:center;padding:10px;background-color:#212121}',
170  
                '#' + fn + 'Box .btn {white-space:nowrap}',
171  
                '#' + fn + 'Box .reject_more {padding:0px 10px;display:none;}',
172  
                '#' + fn + 'Box.reject .reject_more {display:block;}',
173  
                '#' + fn + 'Box.loaded {opacity:0.9;bottom:0px}',
174  
                '#' + fn + 'Box.closed {opacity:0;bottom:-20px}'
175  
            ],
176  
            style2: [
177  
                '#' + fn + 'Box {font-family: Verdana;line-height:24px;color:#f1f1f1;font-size:14px;}',
178  
                '#' + fn + 'Box .btn {text-transform:uppercase;cursor:pointer;background-color:#f1f1f1;color:#659fda;padding:3px 14px;margin-left:10px;}',
179  
                '#' + fn + 'Box .btn:hover {background-color:#ffffff;color:#4d78a5;}',
180  
                '#' + fn + 'Box a {text-decoration:none;color:#659fda}',
181  
            ],
182  
            type: 'text/css',
183  
            element: document.createElement('style'),
184  
            append: function () {
185  
186  
                if (!bootstrap) {
187  
                    this.style = this.style.concat(this.style2);
188  
                }
189  
190  
                this.element.type = this.type;
191  
                this.element.appendChild(document.createTextNode(this.style.join(' ')));
192  
                document.head.insertBefore(this.element, document.head.childNodes[0]);
193  
194  
            }
195  
        };
196  
197  
        css.append();
198  
199  
        // create warning box
200  
        var wbox = document.createElement('div');
201  
        wbox.setAttribute("id", fn + "Box");
202  
203  
        if (classes) {
204  
            wbox.setAttribute("class", classes);
205  
        }
206  
207  
        var args = [
208  
            expire ? expire : 'null',
209  
            path ? "'" + path + "'" : '/',
210  
            domain ? "'" + domain + "'" : 'null',
211  
            secure == "true" ? 1 : 0,
212  
        ].join(',');
213  
214  
        var info = (data.more_link && data.more_text) ? ' <a target="_blank" href="' + data.more_link + '">' + data.more_text + '</a> ' : '';
215  
        var accept_button = '<span class="btn btn-default" id="' + fn + 'Close" onclick="' + fn + '.close(' + args + ');">' + data.accept_text + '</span>';
216  
217  
        if (data.reject_text) {
218  
            var reject_button = '<span class="btn btn-warning" onclick="' + fn + '.reject();">' + data.reject_text + '</span>';
219  
            var reject_content = '<span class="reject_more">' + data.reject_info + ' <a target="_blank" href="' + data.reject_link + '">' + data.reject_link + '</a></span>';
220  
        } else {
221  
            var reject_button = '';
222  
            var reject_content = '';
223  
        }
224  
225  
        wbox.innerHTML = '<div class="text">' + data.text + info + accept_button + reject_button + reject_content + '</div>';
226  
227  
        // append to body
228  
        var body = document.getElementsByTagName("body")[0];
229  
        body.appendChild(wbox);
230  
231  
        setTimeout(function () {
232  
            wbox.className = wbox.className + ' loaded';
233  
        }, delay ? parseInt(delay) : 500);
234  
235  
    }
236  
237  
238  
    var isDOMready = function(){
239  
240  
        if (document.readyState == 'complete') {        
241  
            warn();
242  
        } else {
243  
            setTimeout(function () {
244  
                isDOMready();
245  
            }, 1000);
246  
        }
247  
248  
    };
249  
250  
    isDOMready();
251  
252  
})('cookieWarn');

Author comment

From https://www.cssscript.com/responsive-eu-cookie-law-alert-bar-cookie-warn/

download  show line numbers   

Travelled to 6 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1021514
Snippet name: cookie-warn.js
Eternal ID of this version: #1021514/2
Text MD5: 40223140496ade9b60ac6dea69d96df0
Author: stefan
Category: javax / html
Type: Document
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2019-02-16 00:09:44
Source code size: 8476 bytes / 252 lines
Pitched / IR pitched: No / No
Views / Downloads: 241 / 185
Version history: 1 change(s)
Referenced in: [show references]