Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cookieName configuration #68

Merged
merged 2 commits into from
Oct 26, 2014
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 = 'viewedOuibounceModal';
}

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') {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jdeniau shouldn't this be if (typeof options.cookieName === 'undefined') { use viewedOuibounceModal. Otherwise use options.cookieName?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are totally right.

It's fixed.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! 🍔

cookieName = 'viewedOuibounceModal';
}

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

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