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 canFire option #101

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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 @@ -83,6 +83,7 @@ Ouibounce offers a few options, such as:
- [Timer](#set-a-min-time-before-ouibounce-fires)
- [Delay](#delay)
- [Callback](#callback)
- [CanFire](#canFire)
- [Cookie expiration](#cookie-expiration)
- [Cookie domain](#cookie-domain)
- [Cookie name](#cookie-name)
Expand Down Expand Up @@ -133,6 +134,15 @@ _Example:_
ouibounce(document.getElementById('ouibounce-modal'), { callback: function() { console.log('Ouibounce fired!'); } });
```

##### Canfire
You can add a callback with the `canFire` option, which will check if Ouibounce can be triggered.
By default, this callback returns true;

_Example:_
```js
ouibounce(document.getElementById('ouibounce-modal'), { canFire: function() { console.log("Actually, I don't want to fire Ouibounce!"); return false; } });
```

##### Cookie expiration
Ouibounce sets a cookie by default to prevent the modal from appearing more than once per user. You can add a cookie expiration (in days) using `cookieExpire` to adjust the time period before the modal will appear again for a user. By default, the cookie will expire at the end of the session, which for most browsers is when the browser is closed entirely.

Expand Down
3 changes: 2 additions & 1 deletion build/ouibounce.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ return function ouibounce(el, custom_config) {
sensitivity = setDefault(config.sensitivity, 20),
timer = setDefault(config.timer, 1000),
delay = setDefault(config.delay, 0),
canFire = config.canFire || function() { return true; },
callback = config.callback || function() {},
cookieExpire = setDefaultCookieExpire(config.cookieExpire) || '',
cookieDomain = config.cookieDomain ? ';domain=' + config.cookieDomain : '',
Expand Down Expand Up @@ -94,7 +95,7 @@ return function ouibounce(el, custom_config) {
// You can use ouibounce without passing an element
// https://github.com/carlsednaoui/ouibounce/issues/30
function fire() {
if (isDisabled()) { return; }
if (isDisabled() || !canFire()) { return; }

if (el) { el.style.display = 'block'; }

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.

3 changes: 2 additions & 1 deletion source/ouibounce.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ function ouibounce(el, custom_config) {
sensitivity = setDefault(config.sensitivity, 20),
timer = setDefault(config.timer, 1000),
delay = setDefault(config.delay, 0),
canFire = config.canFire || function() { return true; },
callback = config.callback || function() {},
cookieExpire = setDefaultCookieExpire(config.cookieExpire) || '',
cookieDomain = config.cookieDomain ? ';domain=' + config.cookieDomain : '',
Expand Down Expand Up @@ -82,7 +83,7 @@ function ouibounce(el, custom_config) {
// You can use ouibounce without passing an element
// https://github.com/carlsednaoui/ouibounce/issues/30
function fire() {
if (isDisabled()) { return; }
if (isDisabled() || !canFire()) { return; }

if (el) { el.style.display = 'block'; }

Expand Down