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

Feature/disable popup #30

Open
wants to merge 2 commits 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
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ exit-intent-popup
* Cookie support with optional expiry date.
* Set a timed delay before the script starts tracking exit intent.
* Display popup based on exit intent or timed delay.
* Popup can be enabled and disabled after initialization.
* Scales to adjust to window size.

# Usage
Expand Down Expand Up @@ -36,7 +37,7 @@ You can also add HTML and CSS directly on the page. The popup element you wish
// Options
});
</script>

<style type="text/css">
#bio_ep_bg {} // background
#bio_ep {} // popup
Expand Down Expand Up @@ -106,4 +107,4 @@ Name | Type | Default | Description

MIT license, feel free to use however you wish!

Created by [beeker.io](http://beeker.io/exit-intent-popup-script-tutorial)
Created by [beeker.io](http://beeker.io/exit-intent-popup-script-tutorial)
30 changes: 18 additions & 12 deletions js/bioep.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ window.bioEp = {
popupEl: {},
closeBtnEl: {},
shown: false,
inactive: false,
overflowDefault: "visible",
transformDefault: "",

// Popup options
width: 400,
height: 220,
Expand All @@ -18,45 +19,45 @@ window.bioEp = {
cookieExp: 30,
showOncePerSession: false,
onPopup: null,

// Object for handling cookies, taken from QuirksMode
// http://www.quirksmode.org/js/cookies.html
cookieManager: {
// Create a cookie
create: function(name, value, days, sessionOnly) {
var expires = "";

if(sessionOnly)
expires = "; expires=0"
else if(days) {
var date = new Date();
date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
expires = "; expires=" + date.toGMTString();
}

document.cookie = name + "=" + value + expires + "; path=/";
},

// Get the value of a cookie
get: function(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(";");

for(var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == " ") c = c.substring(1, c.length);
if (c.indexOf(nameEQ) === 0) return c.substring(nameEQ.length, c.length);
}

return null;
},

// Delete a cookie
erase: function(name) {
this.create(name, "", -1);
}
},

// Handle the bioep_shown cookie
// If present and true, return true
// If not present or false, create and return false
Expand Down Expand Up @@ -137,7 +138,7 @@ window.bioEp = {

// Show the popup
showPopup: function() {
if(this.shown) return;
if(this.shown || this.inactive) return;

this.bgEl.style.display = "block";
this.popupEl.style.display = "block";
Expand All @@ -150,10 +151,10 @@ window.bioEp = {
document.body.style.overflow = "hidden";

this.shown = true;

this.cookieManager.create("bioep_shown", "true", this.cookieExp, false);
this.cookieManager.create("bioep_shown_session", "true", 0, true);

if(typeof this.onPopup === "function") {
this.onPopup();
}
Expand Down Expand Up @@ -210,6 +211,11 @@ window.bioEp = {
this.popupEl.style.transform = this.transformDefault + " scale(" + scaleTo + ")";
},

// Toggle whether or not the popup is active
toggleActive: function() {
this.inactive = !this.inactive;
},

// Event listener initialisation for all browsers
addEvent: function (obj, event, callback) {
if(obj.addEventListener)
Expand Down