Skip to content

Commit

Permalink
Merge pull request #68 from jdeniau/cookieName
Browse files Browse the repository at this point in the history
Add cookieName configuration
  • Loading branch information
carlsednaoui committed Oct 26, 2014
2 parents 17e2a68 + 3d6ee63 commit cb57efa
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 9 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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`.

Expand Down Expand Up @@ -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
Expand Down
13 changes: 9 additions & 4 deletions build/ouibounce.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}
Expand All @@ -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;
Expand All @@ -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('=');
Expand Down Expand Up @@ -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 = options.cookieName;
}

document.cookie = cookieName + '=true' + cookieExpire + cookieDomain + sitewide;

// remove listeners
_html.removeEventListener('mouseleave', handleMouseleave);
Expand Down
2 changes: 1 addition & 1 deletion build/ouibounce.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 9 additions & 4 deletions source/ouibounce.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}
Expand All @@ -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;
Expand All @@ -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('=');
Expand Down Expand Up @@ -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 = options.cookieName;
}

document.cookie = cookieName + '=true' + cookieExpire + cookieDomain + sitewide;

// remove listeners
_html.removeEventListener('mouseleave', handleMouseleave);
Expand Down

0 comments on commit cb57efa

Please sign in to comment.