From cd11fc7267994955e7fafd87a5e36d1870655117 Mon Sep 17 00:00:00 2001 From: Julien Deniau Date: Thu, 25 Sep 2014 10:10:21 +0200 Subject: [PATCH 1/2] Add cookieName configuration It may be usefull if you want to add multiple ouibounce on different type of pages. --- README.md | 10 ++++++++++ build/ouibounce.js | 13 +++++++++---- build/ouibounce.min.js | 2 +- source/ouibounce.js | 13 +++++++++---- 4 files changed, 29 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 7181b5e..2690715 100644 --- a/README.md +++ b/README.md @@ -75,6 +75,7 @@ Ouibounce offers a few options, such as: - [Callback](#callback) - [Cookie expiration](#cookie-expiration) - [Cookie domain](#cookie-domain) +- [Cookie name](#cookie-name) - [Sitewide cookie](#sitewide-cookie) - [Chaining options](#chaining-options) @@ -145,6 +146,14 @@ ouibounce(document.getElementById('ouibounce-modal'), { cookieDomain: '.example.com' }); ``` +##### Cookie name +You can specify cookie name passing `cookieName: 'customCookieName'`. + +_Example:_ +```js +ouibounce(document.getElementById('ouibounce-modal'), { cookieName: 'customCookieName' }); +``` + ##### Sitewide cookie You can drop sitewide cookies by using passing `sitewide: true`. @@ -183,6 +192,7 @@ modal.disable({ cookieExpire: 50, sitewide: true }) // disable ouibounce sitewid The `disable` function accepts a few options: - [Cookie expiration](#cookie-expiration) - [Cookie domain](#cookie-domain) +- [Cookie name](#cookie-name) - [Sitewide cookie](#sitewide-cookie) ### Using Ouibounce with other libraries diff --git a/build/ouibounce.js b/build/ouibounce.js index 769ee66..b80b8e6 100644 --- a/build/ouibounce.js +++ b/build/ouibounce.js @@ -19,6 +19,7 @@ return function ouibounce(el, config) { callback = config.callback || function() {}, cookieExpire = setDefaultCookieExpire(config.cookieExpire) || '', cookieDomain = config.cookieDomain ? ';domain=' + config.cookieDomain : '', + cookieName = config.cookieName ? config.cookieName : 'viewedOuibounceModal', sitewide = config.sitewide === true ? ';path=/' : '', _delayTimer = null, _html = document.documentElement; @@ -45,7 +46,7 @@ return function ouibounce(el, config) { } function handleMouseleave(e) { - if (e.clientY > sensitivity || (checkCookieValue('viewedOuibounceModal', 'true') && !aggressive)) return; + if (e.clientY > sensitivity || (checkCookieValue(cookieName, 'true') && !aggressive)) return; _delayTimer = setTimeout(_fireAndCallback, delay); } @@ -59,7 +60,7 @@ return function ouibounce(el, config) { var disableKeydown = false; function handleKeydown(e) { - if (disableKeydown || checkCookieValue('viewedOuibounceModal', 'true') && !aggressive) return; + if (disableKeydown || checkCookieValue(cookieName, 'true') && !aggressive) return; else if(!e.metaKey || e.keyCode !== 76) return; disableKeydown = true; @@ -73,7 +74,7 @@ return function ouibounce(el, config) { function parseCookies() { // cookies are separated by '; ' var cookies = document.cookie.split('; '); - + var ret = {}; for (var i = cookies.length - 1; i >= 0; i--) { var el = cookies[i].split('='); @@ -115,7 +116,11 @@ return function ouibounce(el, config) { cookieDomain = ';domain=' + options.cookieDomain; } - document.cookie = 'viewedOuibounceModal=true' + cookieExpire + cookieDomain + sitewide; + if (typeof options.cookieName !== 'undefined') { + cookieName = 'viewedOuibounceModal'; + } + + document.cookie = cookieName + '=true' + cookieExpire + cookieDomain + sitewide; // remove listeners _html.removeEventListener('mouseleave', handleMouseleave); diff --git a/build/ouibounce.min.js b/build/ouibounce.min.js index 83001ea..60e1792 100644 --- a/build/ouibounce.min.js +++ b/build/ouibounce.min.js @@ -1 +1 @@ -!function(e,n){"function"==typeof define&&define.amd?define(n):"object"==typeof exports?module.exports=n(require,exports,module):e.ouibounce=n()}(this,function(){return function(e,n){function o(e,n){return"undefined"==typeof e?n:e}function t(e){var n=24*e*60*60*1e3,o=new Date;return o.setTime(o.getTime()+n),"; expires="+o.toUTCString()}function i(){T.addEventListener("mouseleave",u),T.addEventListener("mouseenter",r),T.addEventListener("keydown",c)}function u(e){e.clientY>v||d("viewedOuibounceModal","true")&&!l||(x=setTimeout(s,k))}function r(){x&&(clearTimeout(x),x=null)}function c(e){L||d("viewedOuibounceModal","true")&&!l||e.metaKey&&76===e.keyCode&&(L=!0,x=setTimeout(s,k))}function d(e,n){return a()[e]===n}function a(){for(var e=document.cookie.split("; "),n={},o=e.length-1;o>=0;o--){var t=e[o].split("=");n[t[0]]=t[1]}return n}function s(){f(),y()}function f(){e&&(e.style.display="block"),m()}function m(e){var e=e||{};"undefined"!=typeof e.cookieExpire&&(E=t(e.cookieExpire)),e.sitewide===!0&&(w=";path=/"),"undefined"!=typeof e.cookieDomain&&(b=";domain="+e.cookieDomain),document.cookie="viewedOuibounceModal=true"+E+b+w,T.removeEventListener("mouseleave",u),T.removeEventListener("mouseenter",r),T.removeEventListener("keydown",c)}var n=n||{},l=n.aggressive||!1,v=o(n.sensitivity,20),p=o(n.timer,1e3),k=o(n.delay,0),y=n.callback||function(){},E=t(n.cookieExpire)||"",b=n.cookieDomain?";domain="+n.cookieDomain:"",w=n.sitewide===!0?";path=/":"",x=null,T=document.documentElement;setTimeout(i,p);var L=!1;return{fire:f,disable:m}}}); \ No newline at end of file +!function(e,n){"function"==typeof define&&define.amd?define(n):"object"==typeof exports?module.exports=n(require,exports,module):e.ouibounce=n()}(this,function(){return function(e,n){function o(e,n){return"undefined"==typeof e?n:e}function i(e){var n=24*e*60*60*1e3,o=new Date;return o.setTime(o.getTime()+n),"; expires="+o.toUTCString()}function t(){L.addEventListener("mouseleave",u),L.addEventListener("mouseenter",r),L.addEventListener("keydown",c)}function u(e){e.clientY>v||d(w,"true")&&!l||(T=setTimeout(m,k))}function r(){T&&(clearTimeout(T),T=null)}function c(e){g||d(w,"true")&&!l||e.metaKey&&76===e.keyCode&&(g=!0,T=setTimeout(m,k))}function d(e,n){return a()[e]===n}function a(){for(var e=document.cookie.split("; "),n={},o=e.length-1;o>=0;o--){var i=e[o].split("=");n[i[0]]=i[1]}return n}function m(){f(),y()}function f(){e&&(e.style.display="block"),s()}function s(e){var e=e||{};"undefined"!=typeof e.cookieExpire&&(E=i(e.cookieExpire)),e.sitewide===!0&&(x=";path=/"),"undefined"!=typeof e.cookieDomain&&(b=";domain="+e.cookieDomain),"undefined"!=typeof e.cookieName&&(w="viewedOuibounceModal"),document.cookie=w+"=true"+E+b+x,L.removeEventListener("mouseleave",u),L.removeEventListener("mouseenter",r),L.removeEventListener("keydown",c)}var n=n||{},l=n.aggressive||!1,v=o(n.sensitivity,20),p=o(n.timer,1e3),k=o(n.delay,0),y=n.callback||function(){},E=i(n.cookieExpire)||"",b=n.cookieDomain?";domain="+n.cookieDomain:"",w=n.cookieName?n.cookieName:"viewedOuibounceModal",x=n.sitewide===!0?";path=/":"",T=null,L=document.documentElement;setTimeout(t,p);var g=!1;return{fire:f,disable:s}}}); \ No newline at end of file diff --git a/source/ouibounce.js b/source/ouibounce.js index 318d35c..4a0d732 100644 --- a/source/ouibounce.js +++ b/source/ouibounce.js @@ -7,6 +7,7 @@ function ouibounce(el, config) { callback = config.callback || function() {}, cookieExpire = setDefaultCookieExpire(config.cookieExpire) || '', cookieDomain = config.cookieDomain ? ';domain=' + config.cookieDomain : '', + cookieName = config.cookieName ? config.cookieName : 'viewedOuibounceModal', sitewide = config.sitewide === true ? ';path=/' : '', _delayTimer = null, _html = document.documentElement; @@ -33,7 +34,7 @@ function ouibounce(el, config) { } function handleMouseleave(e) { - if (e.clientY > sensitivity || (checkCookieValue('viewedOuibounceModal', 'true') && !aggressive)) return; + if (e.clientY > sensitivity || (checkCookieValue(cookieName, 'true') && !aggressive)) return; _delayTimer = setTimeout(_fireAndCallback, delay); } @@ -47,7 +48,7 @@ function ouibounce(el, config) { var disableKeydown = false; function handleKeydown(e) { - if (disableKeydown || checkCookieValue('viewedOuibounceModal', 'true') && !aggressive) return; + if (disableKeydown || checkCookieValue(cookieName, 'true') && !aggressive) return; else if(!e.metaKey || e.keyCode !== 76) return; disableKeydown = true; @@ -61,7 +62,7 @@ function ouibounce(el, config) { function parseCookies() { // cookies are separated by '; ' var cookies = document.cookie.split('; '); - + var ret = {}; for (var i = cookies.length - 1; i >= 0; i--) { var el = cookies[i].split('='); @@ -103,7 +104,11 @@ function ouibounce(el, config) { cookieDomain = ';domain=' + options.cookieDomain; } - document.cookie = 'viewedOuibounceModal=true' + cookieExpire + cookieDomain + sitewide; + if (typeof options.cookieName !== 'undefined') { + cookieName = 'viewedOuibounceModal'; + } + + document.cookie = cookieName + '=true' + cookieExpire + cookieDomain + sitewide; // remove listeners _html.removeEventListener('mouseleave', handleMouseleave); From 3d6ee63948779a9dfc536c65f17bca342a36c591 Mon Sep 17 00:00:00 2001 From: Julien Deniau Date: Sun, 26 Oct 2014 10:29:14 +0100 Subject: [PATCH 2/2] Fix bug on the "disable" method options --- build/ouibounce.js | 2 +- build/ouibounce.min.js | 2 +- source/ouibounce.js | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/build/ouibounce.js b/build/ouibounce.js index b80b8e6..040b169 100644 --- a/build/ouibounce.js +++ b/build/ouibounce.js @@ -117,7 +117,7 @@ return function ouibounce(el, config) { } if (typeof options.cookieName !== 'undefined') { - cookieName = 'viewedOuibounceModal'; + cookieName = options.cookieName; } document.cookie = cookieName + '=true' + cookieExpire + cookieDomain + sitewide; diff --git a/build/ouibounce.min.js b/build/ouibounce.min.js index 60e1792..d61a9a4 100644 --- a/build/ouibounce.min.js +++ b/build/ouibounce.min.js @@ -1 +1 @@ -!function(e,n){"function"==typeof define&&define.amd?define(n):"object"==typeof exports?module.exports=n(require,exports,module):e.ouibounce=n()}(this,function(){return function(e,n){function o(e,n){return"undefined"==typeof e?n:e}function i(e){var n=24*e*60*60*1e3,o=new Date;return o.setTime(o.getTime()+n),"; expires="+o.toUTCString()}function t(){L.addEventListener("mouseleave",u),L.addEventListener("mouseenter",r),L.addEventListener("keydown",c)}function u(e){e.clientY>v||d(w,"true")&&!l||(T=setTimeout(m,k))}function r(){T&&(clearTimeout(T),T=null)}function c(e){g||d(w,"true")&&!l||e.metaKey&&76===e.keyCode&&(g=!0,T=setTimeout(m,k))}function d(e,n){return a()[e]===n}function a(){for(var e=document.cookie.split("; "),n={},o=e.length-1;o>=0;o--){var i=e[o].split("=");n[i[0]]=i[1]}return n}function m(){f(),y()}function f(){e&&(e.style.display="block"),s()}function s(e){var e=e||{};"undefined"!=typeof e.cookieExpire&&(E=i(e.cookieExpire)),e.sitewide===!0&&(x=";path=/"),"undefined"!=typeof e.cookieDomain&&(b=";domain="+e.cookieDomain),"undefined"!=typeof e.cookieName&&(w="viewedOuibounceModal"),document.cookie=w+"=true"+E+b+x,L.removeEventListener("mouseleave",u),L.removeEventListener("mouseenter",r),L.removeEventListener("keydown",c)}var n=n||{},l=n.aggressive||!1,v=o(n.sensitivity,20),p=o(n.timer,1e3),k=o(n.delay,0),y=n.callback||function(){},E=i(n.cookieExpire)||"",b=n.cookieDomain?";domain="+n.cookieDomain:"",w=n.cookieName?n.cookieName:"viewedOuibounceModal",x=n.sitewide===!0?";path=/":"",T=null,L=document.documentElement;setTimeout(t,p);var g=!1;return{fire:f,disable:s}}}); \ No newline at end of file +!function(e,n){"function"==typeof define&&define.amd?define(n):"object"==typeof exports?module.exports=n(require,exports,module):e.ouibounce=n()}(this,function(){return function(e,n){function o(e,n){return"undefined"==typeof e?n:e}function t(e){var n=24*e*60*60*1e3,o=new Date;return o.setTime(o.getTime()+n),"; expires="+o.toUTCString()}function i(){L.addEventListener("mouseleave",u),L.addEventListener("mouseenter",r),L.addEventListener("keydown",c)}function u(e){e.clientY>v||d(T,"true")&&!l||(w=setTimeout(m,p))}function r(){w&&(clearTimeout(w),w=null)}function c(e){g||d(T,"true")&&!l||e.metaKey&&76===e.keyCode&&(g=!0,w=setTimeout(m,p))}function d(e,n){return a()[e]===n}function a(){for(var e=document.cookie.split("; "),n={},o=e.length-1;o>=0;o--){var t=e[o].split("=");n[t[0]]=t[1]}return n}function m(){f(),y()}function f(){e&&(e.style.display="block"),s()}function s(e){var e=e||{};"undefined"!=typeof e.cookieExpire&&(E=t(e.cookieExpire)),e.sitewide===!0&&(b=";path=/"),"undefined"!=typeof e.cookieDomain&&(x=";domain="+e.cookieDomain),"undefined"!=typeof e.cookieName&&(T=e.cookieName),document.cookie=T+"=true"+E+x+b,L.removeEventListener("mouseleave",u),L.removeEventListener("mouseenter",r),L.removeEventListener("keydown",c)}var n=n||{},l=n.aggressive||!1,v=o(n.sensitivity,20),k=o(n.timer,1e3),p=o(n.delay,0),y=n.callback||function(){},E=t(n.cookieExpire)||"",x=n.cookieDomain?";domain="+n.cookieDomain:"",T=n.cookieName?n.cookieName:"viewedOuibounceModal",b=n.sitewide===!0?";path=/":"",w=null,L=document.documentElement;setTimeout(i,k);var g=!1;return{fire:f,disable:s}}}); \ No newline at end of file diff --git a/source/ouibounce.js b/source/ouibounce.js index 4a0d732..02a4fb9 100644 --- a/source/ouibounce.js +++ b/source/ouibounce.js @@ -105,7 +105,7 @@ function ouibounce(el, config) { } if (typeof options.cookieName !== 'undefined') { - cookieName = 'viewedOuibounceModal'; + cookieName = options.cookieName; } document.cookie = cookieName + '=true' + cookieExpire + cookieDomain + sitewide;