Having problems getting an Exit Intent Popup to work on a WordPress based site. Site is currently at http://192.249.114.220/~veilco5/ and the error is in the ysExit.js file as follows:
ysExit.js:145 Uncaught TypeError: Cannot read property 'hasOwnProperty' of undefined
That references it to:
Object.keys(defaults).forEach(function (key) {
newOptions[key] = options.hasOwnProperty(key) ? options[key] : defaults[key];
});
I think part of the problem was the original script used $ and I know you can't use that on WP sites. So I switched $ to jQuery in most cases, but not all. Maybe someone can point me to getting the proper references to load and define the script. Here's the code:
(function (jQuery) {
"use strict";
jQuery.fn.ysExit = function (o) {
var $self = this;
var defaults = {
width: '40%', //popup width
height: '30%', //popup height
target: '#ys-container', //popup selector
cookieValidity: 1, //days
closeOnOutsideClick: true, //close popup if user clicks outside
delay: 0, //delay in ms until the popup is registered
debug: false, //if true, the cookie will not be set
cookieName: 'ysExit'
},
content = insertContent(),
options = fixOptions(o),
element = document.querySelector(options.target),
layer = document.querySelector('.ys-layer'),
closeBt = document.querySelector(options.target + ' .ys-popup-close'),
inner = document.querySelector(options.target + ' .ys-box'),
exitBt = document.querySelector(options.target + ' .ys-exit'),
//cookies management helper
cookies = {
set: function (name, value, days) {
var components = [name + '=' + value];
if (days) {
var date = new Date();
date.setTime(date.getTime() + (days * 24 * 3600 * 1000));
components.push('expires=' + date.toGMTString());
}
components.push('path=/');
document.cookie = components.join('; ');
},
get: function (name) {
var start = name + '=',
arr = document.cookie.split(';'),
i;
for (i = 0; i < arr.length; i++) {
var item = arr[i].trim();
if (item.indexOf(start) === 0) {
return item.substring(start.length);
}
}
return null;
}
},
//the popup object
pop = {
active: false,
mouseLeftWindow: function (e) {
var from, to;
e = e ? e : window.event;
from = e.relatedTarget || e.toElement;
if (!from || from.nodeName === "HTML") {
pop.open();
}
},
setDimension: function (dimension, value) {
if (value.toString().indexOf('%') === -1) {
value = value + 'px';
}
inner.style[dimension] = value;
},
attachEvents: function () {
function close(e) {
pop.destroy();
e.preventDefault();
}
document.addEventListener('mouseout', pop.mouseLeftWindow, false);
closeBt.addEventListener('click', close);
if (exitBt) {
exitBt.addEventListener('click', close);
}
if (options.closeOnOutsideClick) {
element.addEventListener('click', close);
inner.addEventListener('click', function (e) {
e.stopPropagation();
});
}
this.active = true;
},
detachEvents: function () {
document.removeEventListener('mouseout', pop.mouseLeftWindow);
},
open: function () {
var self = this;
element.classList.add('visible');
layer.classList.add('visible');
self.detachEvents();
setTimeout(function () {
self.setDimension('width', options.width);
self.setDimension('height', 'auto');
}, 20);
setTimeout(function () {
element.classList.add('finished');
}, 200);
},
destroy: function () {
if (this.active) {
pop.detachEvents();
setTimeout(function () {
element.parentNode.removeChild(element);
layer.classList.remove('visible');
}, 200);
if (!options.debug) {
cookies.set(options.cookieName, 1, options.cookieValidity);
}
}
}
};
function insertContent() {
// console.log($self);
var body = jQuery('body').append('<div class="ys-layer"></div> <div class="ys-container" id="ys-container"><div class="ys-box"><a class="ys-popup-close" href="#">x</a><div class="ys-popup-content"></div></div></div>');
jQuery('.ys-popup-content').append($self[0].outerHTML);
$self.hide();
return true;
}
//helper functions
function fixOptions(options) {
var newOptions = {};
Object.keys(defaults).forEach(function (key) {
newOptions[key] = options.hasOwnProperty(key) ? options[key] : defaults[key];
});
return newOptions;
}
function delegate(obj, func) {
return function () {
func.apply(obj, arguments);
};
}
//start logic
if (!cookies.get(options.cookieName)) {
if (typeof options.delay !== 'number') {
throw new Error('options.delay must be a numeric value');
}
if (!element) {
throw new Error('Could not find element with selector: ' + options.target);
}
if (element.parentNode !== document.body) {
throw new Error(options.target + ' element must be placed directly inside of the <body> element');
}
setTimeout(delegate(pop, pop.attachEvents), options.delay);
}
//return object with public interface
return {
open: delegate(pop, pop.open),
destroy: delegate(pop, pop.destroy),
getElement: function () {
return element;
}
};
};
})(jQuery);
ysExit.js:145 Uncaught TypeError: Cannot read property 'hasOwnProperty' of undefined
To fix that error, choose one of these options:
Call the ysExit()
function with an empty object
like this: ysExit({})
instead of just ysExit()
In the plugin, change the options = fixOptions(o),
to options = fixOptions(o || {}),
In the plugin, change the options = fixOptions(o),
to options = jQuery.extend(o, defaults),
(and then remove the fixOptions()
function).
In addition, from what I've tested, the popup element has to be an immediate child of the body
element. I.e.
<body>
<div id="popup">...</div>
</body>
.. and not:
<body>
<div class="some-class">
<div id="popup">...</div>
</div>
</body>
So here are the HTML and JS I used when testing:
<div id="popup-demo"><!-- Direct `body` child -->
<h3>Popup Demo</h3>
<p>Content goes here...</p>
</div>
<script>
jQuery('#popup-demo').ysExit({});
</script>