Skip to content

Commit

Permalink
Minor JS enhancements
Browse files Browse the repository at this point in the history
- No longer use `reduce`
- Cleaner way to select the HTML
- Better assertion for keyCode
- Close #53
  • Loading branch information
carlsednaoui committed Aug 17, 2014
1 parent 35bfed0 commit 2cc551c
Showing 1 changed file with 14 additions and 13 deletions.
27 changes: 14 additions & 13 deletions source/ouibounce.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ function ouibounce(el, config) {
cookieDomain = config.cookieDomain ? ';domain=' + config.cookieDomain : '',
sitewide = config.sitewide === true ? ';path=/' : '',
_delayTimer = null,
_html = document.getElementsByTagName('html')[0];
_html = document.documentElement;

function setDefault(_property, _default) {
return typeof _property === 'undefined' ? _default : _property;
Expand Down Expand Up @@ -48,25 +48,26 @@ function ouibounce(el, config) {
var disableKeydown = false;
function handleKeydown(e) {
if (disableKeydown || checkCookieValue('viewedOuibounceModal', 'true') && !aggressive) return;
else if(!e.metaKey || e.keyCode != 76) return;
else if(!e.metaKey || e.keyCode !== 76) return;

disableKeydown = true;
_delayTimer = setTimeout(_fireAndCallback, delay);
}

function checkCookieValue(cookieName, value) {
// cookies are separated by '; '
var cookies = document.cookie.split('; ').reduce(function(prev, curr) {
// split by '=' to get key, value pairs
var el = curr.split('=');

// add the cookie to fn object
prev[el[0]] = el[1];

return prev;
}, {});
return parseCookies()[cookieName] === value;
}

return cookies[cookieName] === value;
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('=');
ret[el[0]] = el[1];
}
return ret;
}

function _fireAndCallback() {
Expand Down

0 comments on commit 2cc551c

Please sign in to comment.