From 0d3498c2ba714377b15e4aa2079e61d08e9ae74c Mon Sep 17 00:00:00 2001 From: Luis Merino Date: Tue, 13 May 2014 19:11:19 +0200 Subject: [PATCH 1/4] Add config to allow subdomain cookies --- build/ouibounce.js | 11 +++++++++-- build/ouibounce.min.js | 2 +- source/ouibounce.js | 11 +++++++++-- test/ouibounce.min.css | 1 - 4 files changed, 19 insertions(+), 6 deletions(-) delete mode 100644 test/ouibounce.min.css diff --git a/build/ouibounce.js b/build/ouibounce.js index 12ef79f..5d72ab0 100644 --- a/build/ouibounce.js +++ b/build/ouibounce.js @@ -17,6 +17,7 @@ return function ouibounce(el, config) { timer = setDefault(config.timer, 1000), callback = config.callback || function() {}, cookieExpire = setDefaultCookieExpire(config.cookieExpire) || '', + cookieDomain = config.cookieDomain ? ';domain=' + config.cookieDomain : '', sitewide = config.sitewide === true ? ';path=/' : '', _html = document.getElementsByTagName('html')[0]; @@ -93,8 +94,14 @@ return function ouibounce(el, config) { sitewide = ';path=/'; } - document.cookie = 'viewedOuibounceModal=true' + cookieExpire + sitewide; - + // you can pass a domain string, for example when the cookie should be + // read subdomain-wise, e.g.: .example.com + if (typeof options.cookieDomain !== 'undefined') { + cookieDomain = options.cookieDomain; + } + + document.cookie = 'viewedOuibounceModal=true' + cookieExpire + ookieDomain + sitewide; + // remove listeners _html.removeEventListener('mouseout', handleMouseout); _html.removeEventListener('keydown', handleKeydown); diff --git a/build/ouibounce.min.js b/build/ouibounce.min.js index 6c3f4bd..f0c89ea 100644 --- a/build/ouibounce.min.js +++ b/build/ouibounce.min.js @@ -1 +1 @@ -!function(e,t){"function"==typeof define&&define.amd?define(t):"object"==typeof exports?module.exports=t(require,exports,module):e.ouibounce=t()}(this,function(){return function(e,t){function n(e,t){return"undefined"==typeof e?t:e}function i(e){var t=24*e*60*60*1e3,n=new Date;return n.setTime(n.getTime()+t),"; expires="+n.toGMTString()}function o(){y.addEventListener("mouseout",u),y.addEventListener("keydown",r)}function u(e){e.clientY>a||d("viewedOuibounceModal","true")&&!s||(c(),p())}function r(e){k||d("viewedOuibounceModal","true")&&!s||e.metaKey&&76==e.keyCode&&(k=!0,c(),p())}function d(e,t){var n=document.cookie.split("; ").reduce(function(e,t){var n=t.split("=");return e[n[0]]=n[1],e},{});return n[e]===t}function c(){e&&(e.style.display="block"),f()}function f(e){var e=e||{};"undefined"!=typeof e.cookieExpire&&(v=i(e.cookieExpire)),"undefined"!=typeof e.sitewide&&(l=";path=/"),document.cookie="viewedOuibounceModal=true"+v+l,y.removeEventListener("mouseout",u),y.removeEventListener("keydown",r)}var t=t||{},s=t.aggressive||!1,a=n(t.sensitivity,20),m=n(t.timer,1e3),p=t.callback||function(){},v=i(t.cookieExpire)||"",l=t.sitewide===!0?";path=/":"",y=document.getElementsByTagName("html")[0];setTimeout(o,m);var k=!1;return{fire:c,disable:f}}}); \ No newline at end of file +!function(e,o){"function"==typeof define&&define.amd?define(o):"object"==typeof exports?module.exports=o(require,exports,module):e.ouibounce=o()}(this,function(){return function(e,o){function i(e,o){return"undefined"==typeof e?o:e}function n(e){var o=24*e*60*60*1e3,i=new Date;return i.setTime(i.getTime()+o),"; expires="+i.toGMTString()}function t(){y.addEventListener("mouseout",u),y.addEventListener("keydown",r)}function u(e){e.clientY>s||c("viewedOuibounceModal","true")&&!f||(d(),p())}function r(e){b||c("viewedOuibounceModal","true")&&!f||e.metaKey&&76==e.keyCode&&(b=!0,d(),p())}function c(e,o){var i=document.cookie.split("; ").reduce(function(e,o){var i=o.split("=");return e[i[0]]=i[1],e},{});return i[e]===o}function d(){e&&(e.style.display="block"),a()}function a(e){var e=e||{};"undefined"!=typeof e.cookieExpire&&(v=n(e.cookieExpire)),"undefined"!=typeof e.sitewide&&(k=";path=/"),"undefined"!=typeof e.cookieDomain&&(l=e.cookieDomain),document.cookie="viewedOuibounceModal=true"+v+ookieDomain+k,y.removeEventListener("mouseout",u),y.removeEventListener("keydown",r)}var o=o||{},f=o.aggressive||!1,s=i(o.sensitivity,20),m=i(o.timer,1e3),p=o.callback||function(){},v=n(o.cookieExpire)||"",l=o.cookieDomain?";domain="+o.cookieDomain:"",k=o.sitewide===!0?";path=/":"",y=document.getElementsByTagName("html")[0];setTimeout(t,m);var b=!1;return{fire:d,disable:a}}}); \ No newline at end of file diff --git a/source/ouibounce.js b/source/ouibounce.js index b4f1a2e..ffa168c 100644 --- a/source/ouibounce.js +++ b/source/ouibounce.js @@ -5,6 +5,7 @@ function ouibounce(el, config) { timer = setDefault(config.timer, 1000), callback = config.callback || function() {}, cookieExpire = setDefaultCookieExpire(config.cookieExpire) || '', + cookieDomain = config.cookieDomain ? ';domain=' + config.cookieDomain : '', sitewide = config.sitewide === true ? ';path=/' : '', _html = document.getElementsByTagName('html')[0]; @@ -81,8 +82,14 @@ function ouibounce(el, config) { sitewide = ';path=/'; } - document.cookie = 'viewedOuibounceModal=true' + cookieExpire + sitewide; - + // you can pass a domain string, for example when the cookie should be + // read subdomain-wise, e.g.: .example.com + if (typeof options.cookieDomain !== 'undefined') { + cookieDomain = options.cookieDomain; + } + + document.cookie = 'viewedOuibounceModal=true' + cookieExpire + ookieDomain + sitewide; + // remove listeners _html.removeEventListener('mouseout', handleMouseout); _html.removeEventListener('keydown', handleKeydown); diff --git a/test/ouibounce.min.css b/test/ouibounce.min.css deleted file mode 100644 index 041565f..0000000 --- a/test/ouibounce.min.css +++ /dev/null @@ -1 +0,0 @@ -#ouibounce-modal{font-family:'Open Sans',sans-serif;display:none;position:fixed;top:0;left:0;width:100%;height:100%}#ouibounce-modal .underlay{width:100%;height:100%;position:absolute;top:0;left:0;background-color:rgba(0,0,0,.5);cursor:pointer;-webkit-animation:fadein .5s;animation:fadein .5s}#ouibounce-modal .modal{width:600px;height:400px;background-color:#f0f1f2;z-index:1;position:absolute;margin:auto;top:0;right:0;bottom:0;left:0;border-radius:4px;-webkit-animation:popin .3s;animation:popin .3s}#ouibounce-modal .modal-title{font-size:18px;background-color:#252525;color:#fff;padding:10px;margin:0;border-radius:4px 4px 0 0;text-align:center}#ouibounce-modal h3{color:#fff;font-size:1em;margin:.2em;text-transform:uppercase;font-weight:500}#ouibounce-modal .modal-body{padding:20px 35px;font-size:.9em}#ouibounce-modal p{color:#344a5f}#ouibounce-modal form{text-align:center;margin-top:35px}#ouibounce-modal form input[type=text]{padding:12px;font-size:1.2em;width:300px;border-radius:4px;border:1px solid #ccc;-webkit-font-smoothing:antialiased}#ouibounce-modal form input[type=submit]{text-transform:uppercase;font-weight:700;padding:12px;font-size:1.1em;border-radius:4px;color:#fff;background-color:#4ab471;border:0;cursor:pointer;-webkit-font-smoothing:antialiased}#ouibounce-modal form p{text-align:left;margin-left:35px;opacity:.8;margin-top:1px;padding-top:1px;font-size:.9em}#ouibounce-modal .modal-footer{position:absolute;bottom:20px;text-align:center;width:100%}#ouibounce-modal .modal-footer p{text-transform:capitalize;cursor:pointer;display:inline;border-bottom:1px solid #344a5f}@-webkit-keyframes fadein{0%{opacity:0}100%{opacity:1}}@-ms-keyframes fadein{0%{opacity:0}100%{opacity:1}}@keyframes fadein{0%{opacity:0}100%{opacity:1}}@-webkit-keyframes popin{0%{-webkit-transform:scale(0);transform:scale(0);opacity:0}85%{-webkit-transform:scale(1.05);transform:scale(1.05);opacity:1}100%{-webkit-transform:scale(1);transform:scale(1);opacity:1}}@-ms-keyframes popin{0%{-ms-transform:scale(0);transform:scale(0);opacity:0}85%{-ms-transform:scale(1.05);transform:scale(1.05);opacity:1}100%{-ms-transform:scale(1);transform:scale(1);opacity:1}}@keyframes popin{0%{-webkit-transform:scale(0);-ms-transform:scale(0);transform:scale(0);opacity:0}85%{-webkit-transform:scale(1.05);-ms-transform:scale(1.05);transform:scale(1.05);opacity:1}100%{-webkit-transform:scale(1);-ms-transform:scale(1);transform:scale(1);opacity:1}} \ No newline at end of file From eacb3819ba228ccb66209bf1116a2c57f9888bfd Mon Sep 17 00:00:00 2001 From: Luis Merino Date: Tue, 13 May 2014 21:01:31 +0200 Subject: [PATCH 2/4] Corrects typo, and adds config readme docs --- README.md | 17 +++++++++++++++++ build/ouibounce.js | 3 ++- build/ouibounce.min.js | 2 +- source/ouibounce.js | 3 ++- 4 files changed, 22 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index d239fe1..b148ce0 100644 --- a/README.md +++ b/README.md @@ -64,6 +64,7 @@ OuiBounce offers a few options, such as: - [Timer](#set-a-min-time-before-ouibounce-fires) - [Callback](#callback) - [Cookie expiration](#cookie-expiration) +- [Cookie domain](#cookie-domain) - [Sitewide cookie](#sitewide-cookie) - [Chaining options](#chaining-options) @@ -109,6 +110,21 @@ _Example:_ ouibounce(document.getElementById('ouibounce-modal'), { cookieExpire: 10 }); ``` +##### Cookie domain +Ouibounce sets a cookie by default to prevent the modal from appearing +more than once per user. You can add a cookie domain using +`cookieDomain` to specify the domain under which the cookie should work. +By default, no extra domain information will be added. If you need a +cookie to work also in your subdomain (like blog.example.com and +example.com), then set a cookieDomain such as .example.com (notice the +dot in front). + +_Example:_ +```js +ouibounce(document.getElementById('ouibounce-modal'), { cookieDomain: +'.example.com' }); +``` + ##### Sitewide cookie You can drop sitewide cookies by using this option. @@ -125,6 +141,7 @@ _Example:_ ouibounce(document.getElementById('ouibounce-modal'), { aggressive: true, sitewide: true, + cookieDomain: '.example.com', timer: 0, callback: function() { console.log('ouibounce fired!'); } }); diff --git a/build/ouibounce.js b/build/ouibounce.js index 5d72ab0..5326d99 100644 --- a/build/ouibounce.js +++ b/build/ouibounce.js @@ -100,7 +100,7 @@ return function ouibounce(el, config) { cookieDomain = options.cookieDomain; } - document.cookie = 'viewedOuibounceModal=true' + cookieExpire + ookieDomain + sitewide; + document.cookie = 'viewedOuibounceModal=true' + cookieExpire + cookieDomain + sitewide; // remove listeners _html.removeEventListener('mouseout', handleMouseout); @@ -112,6 +112,7 @@ return function ouibounce(el, config) { disable: disable }; } + ; })); diff --git a/build/ouibounce.min.js b/build/ouibounce.min.js index f0c89ea..b126916 100644 --- a/build/ouibounce.min.js +++ b/build/ouibounce.min.js @@ -1 +1 @@ -!function(e,o){"function"==typeof define&&define.amd?define(o):"object"==typeof exports?module.exports=o(require,exports,module):e.ouibounce=o()}(this,function(){return function(e,o){function i(e,o){return"undefined"==typeof e?o:e}function n(e){var o=24*e*60*60*1e3,i=new Date;return i.setTime(i.getTime()+o),"; expires="+i.toGMTString()}function t(){y.addEventListener("mouseout",u),y.addEventListener("keydown",r)}function u(e){e.clientY>s||c("viewedOuibounceModal","true")&&!f||(d(),p())}function r(e){b||c("viewedOuibounceModal","true")&&!f||e.metaKey&&76==e.keyCode&&(b=!0,d(),p())}function c(e,o){var i=document.cookie.split("; ").reduce(function(e,o){var i=o.split("=");return e[i[0]]=i[1],e},{});return i[e]===o}function d(){e&&(e.style.display="block"),a()}function a(e){var e=e||{};"undefined"!=typeof e.cookieExpire&&(v=n(e.cookieExpire)),"undefined"!=typeof e.sitewide&&(k=";path=/"),"undefined"!=typeof e.cookieDomain&&(l=e.cookieDomain),document.cookie="viewedOuibounceModal=true"+v+ookieDomain+k,y.removeEventListener("mouseout",u),y.removeEventListener("keydown",r)}var o=o||{},f=o.aggressive||!1,s=i(o.sensitivity,20),m=i(o.timer,1e3),p=o.callback||function(){},v=n(o.cookieExpire)||"",l=o.cookieDomain?";domain="+o.cookieDomain:"",k=o.sitewide===!0?";path=/":"",y=document.getElementsByTagName("html")[0];setTimeout(t,m);var b=!1;return{fire:d,disable:a}}}); \ 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.toGMTString()}function t(){y.addEventListener("mouseout",u),y.addEventListener("keydown",r)}function u(e){e.clientY>s||c("viewedOuibounceModal","true")&&!f||(d(),p())}function r(e){b||c("viewedOuibounceModal","true")&&!f||e.metaKey&&76==e.keyCode&&(b=!0,d(),p())}function c(e,n){var o=document.cookie.split("; ").reduce(function(e,n){var o=n.split("=");return e[o[0]]=o[1],e},{});return o[e]===n}function d(){e&&(e.style.display="block"),a()}function a(e){var e=e||{};"undefined"!=typeof e.cookieExpire&&(v=i(e.cookieExpire)),"undefined"!=typeof e.sitewide&&(k=";path=/"),"undefined"!=typeof e.cookieDomain&&(l=e.cookieDomain),document.cookie="viewedOuibounceModal=true"+v+l+k,y.removeEventListener("mouseout",u),y.removeEventListener("keydown",r)}var n=n||{},f=n.aggressive||!1,s=o(n.sensitivity,20),m=o(n.timer,1e3),p=n.callback||function(){},v=i(n.cookieExpire)||"",l=n.cookieDomain?";domain="+n.cookieDomain:"",k=n.sitewide===!0?";path=/":"",y=document.getElementsByTagName("html")[0];setTimeout(t,m);var b=!1;return{fire:d,disable:a}}}); \ No newline at end of file diff --git a/source/ouibounce.js b/source/ouibounce.js index ffa168c..d1e3d09 100644 --- a/source/ouibounce.js +++ b/source/ouibounce.js @@ -88,7 +88,7 @@ function ouibounce(el, config) { cookieDomain = options.cookieDomain; } - document.cookie = 'viewedOuibounceModal=true' + cookieExpire + ookieDomain + sitewide; + document.cookie = 'viewedOuibounceModal=true' + cookieExpire + cookieDomain + sitewide; // remove listeners _html.removeEventListener('mouseout', handleMouseout); @@ -100,3 +100,4 @@ function ouibounce(el, config) { disable: disable }; } + From af4938703f7e33fd359b5e8d678f90de8dff74c3 Mon Sep 17 00:00:00 2001 From: carl sednaoui Date: Tue, 13 May 2014 20:49:25 -0400 Subject: [PATCH 3/4] Fix PR bug in options.cookieDomain --- build/ouibounce.js | 6 +++--- build/ouibounce.min.js | 2 +- source/ouibounce.js | 7 +++---- 3 files changed, 7 insertions(+), 8 deletions(-) diff --git a/build/ouibounce.js b/build/ouibounce.js index d762ba5..60c3c7b 100644 --- a/build/ouibounce.js +++ b/build/ouibounce.js @@ -94,10 +94,10 @@ return function ouibounce(el, config) { sitewide = ';path=/'; } - // you can pass a domain string, for example when the cookie should be - // read subdomain-wise, e.g.: .example.com + // you can pass a domain string when the cookie should be read subdomain-wise + // ex: _ouiBounce.disable({ cookieDomain: '.example.com' }); if (typeof options.cookieDomain !== 'undefined') { - cookieDomain = options.cookieDomain; + cookieDomain = ';domain=' + options.cookieDomain; } document.cookie = 'viewedOuibounceModal=true' + cookieExpire + cookieDomain + sitewide; diff --git a/build/ouibounce.min.js b/build/ouibounce.min.js index 5ee863c..b032c53 100644 --- a/build/ouibounce.min.js +++ b/build/ouibounce.min.js @@ -1 +1 @@ -!function(e,o){"function"==typeof define&&define.amd?define(o):"object"==typeof exports?module.exports=o(require,exports,module):e.ouibounce=o()}(this,function(){return function(e,o){function i(e,o){return"undefined"==typeof e?o:e}function n(e){var o=24*e*60*60*1e3,i=new Date;return i.setTime(i.getTime()+o),"; expires="+i.toGMTString()}function t(){y.addEventListener("mouseout",u),y.addEventListener("keydown",r)}function u(e){e.clientY>s||c("viewedOuibounceModal","true")&&!f||(d(),p())}function r(e){b||c("viewedOuibounceModal","true")&&!f||e.metaKey&&76==e.keyCode&&(b=!0,d(),p())}function c(e,o){var i=document.cookie.split("; ").reduce(function(e,o){var i=o.split("=");return e[i[0]]=i[1],e},{});return i[e]===o}function d(){e&&(e.style.display="block"),a()}function a(e){var e=e||{};"undefined"!=typeof e.cookieExpire&&(v=n(e.cookieExpire)),e.sitewide===!0&&(k=";path=/"),"undefined"!=typeof e.cookieDomain&&(l=e.cookieDomain),document.cookie="viewedOuibounceModal=true"+v+l+k,y.removeEventListener("mouseout",u),y.removeEventListener("keydown",r)}var o=o||{},f=o.aggressive||!1,s=i(o.sensitivity,20),m=i(o.timer,1e3),p=o.callback||function(){},v=n(o.cookieExpire)||"",l=o.cookieDomain?";domain="+o.cookieDomain:"",k=o.sitewide===!0?";path=/":"",y=document.getElementsByTagName("html")[0];setTimeout(t,m);var b=!1;return{fire:d,disable:a}}}); \ No newline at end of file +!function(e,o){"function"==typeof define&&define.amd?define(o):"object"==typeof exports?module.exports=o(require,exports,module):e.ouibounce=o()}(this,function(){return function(e,o){function i(e,o){return"undefined"==typeof e?o:e}function n(e){var o=24*e*60*60*1e3,i=new Date;return i.setTime(i.getTime()+o),"; expires="+i.toGMTString()}function t(){y.addEventListener("mouseout",u),y.addEventListener("keydown",r)}function u(e){e.clientY>s||c("viewedOuibounceModal","true")&&!f||(d(),p())}function r(e){b||c("viewedOuibounceModal","true")&&!f||e.metaKey&&76==e.keyCode&&(b=!0,d(),p())}function c(e,o){var i=document.cookie.split("; ").reduce(function(e,o){var i=o.split("=");return e[i[0]]=i[1],e},{});return i[e]===o}function d(){e&&(e.style.display="block"),a()}function a(e){var e=e||{};"undefined"!=typeof e.cookieExpire&&(v=n(e.cookieExpire)),e.sitewide===!0&&(k=";path=/"),"undefined"!=typeof e.cookieDomain&&(l=";domain="+e.cookieDomain),document.cookie="viewedOuibounceModal=true"+v+l+k,y.removeEventListener("mouseout",u),y.removeEventListener("keydown",r)}var o=o||{},f=o.aggressive||!1,s=i(o.sensitivity,20),m=i(o.timer,1e3),p=o.callback||function(){},v=n(o.cookieExpire)||"",l=o.cookieDomain?";domain="+o.cookieDomain:"",k=o.sitewide===!0?";path=/":"",y=document.getElementsByTagName("html")[0];setTimeout(t,m);var b=!1;return{fire:d,disable:a}}}); \ No newline at end of file diff --git a/source/ouibounce.js b/source/ouibounce.js index a2bfdb4..4816528 100644 --- a/source/ouibounce.js +++ b/source/ouibounce.js @@ -82,10 +82,10 @@ function ouibounce(el, config) { sitewide = ';path=/'; } - // you can pass a domain string, for example when the cookie should be - // read subdomain-wise, e.g.: .example.com + // you can pass a domain string when the cookie should be read subdomain-wise + // ex: _ouiBounce.disable({ cookieDomain: '.example.com' }); if (typeof options.cookieDomain !== 'undefined') { - cookieDomain = options.cookieDomain; + cookieDomain = ';domain=' + options.cookieDomain; } document.cookie = 'viewedOuibounceModal=true' + cookieExpire + cookieDomain + sitewide; @@ -100,4 +100,3 @@ function ouibounce(el, config) { disable: disable }; } - From 07513ddb33f0681de9e6dc00689d65b4e5e33f8a Mon Sep 17 00:00:00 2001 From: carl sednaoui Date: Tue, 13 May 2014 20:51:22 -0400 Subject: [PATCH 4/4] Add disable options explanation in README --- README.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/README.md b/README.md index e80145c..80279db 100644 --- a/README.md +++ b/README.md @@ -159,6 +159,12 @@ modal.disable() // disable ouibounce, it will not fire on page exit modal.disable({ cookieExpire: 50, sitewide: true }) // disable ouibounce sitewide for 50 days. ``` +##### Disable options +The `disable` function accepts a few options: +- [Cookie expiration](#cookie-expiration) +- [Cookie domain](#cookie-domain) +- [Sitewide cookie](#sitewide-cookie) + ### Using Ouibounce with other libraries If you want to use this library with other plugins — such as [Vex](http://github.hubspot.com/vex/docs/welcome/) — you can call ouibounce with `false`. See [#30](https://github.com/carlsednaoui/ouibounce/issues/30) for discussion.