Jquery 使用Exit internt弹出窗口在WordPress站点上出现JS错误

Jquery 使用Exit internt弹出窗口在WordPress站点上出现JS错误,jquery,wordpress,popup,exit,exit-intent,Jquery,Wordpress,Popup,Exit,Exit Intent,在基于WordPress的网站上获取退出意图弹出窗口时遇到问题。站点当前位于,错误在ysExit.js文件中,如下所示: ysExit.js:145未捕获类型错误:无法读取未定义的属性“hasOwnProperty” 它提到: Object.keys(defaults).forEach(function (key) { newOptions[key] = options.hasOwnProperty(key) ? options[key] : defaults[k

在基于WordPress的网站上获取退出意图弹出窗口时遇到问题。站点当前位于,错误在ysExit.js文件中,如下所示:

ysExit.js:145未捕获类型错误:无法读取未定义的属性“hasOwnProperty”

它提到:

Object.keys(defaults).forEach(function (key) {
                newOptions[key] = options.hasOwnProperty(key) ? options[key] : defaults[key];
           });
我认为问题的一部分是原始脚本使用了$,我知道你不能在WP网站上使用它。所以在大多数情况下,我将$切换到jQuery,但不是全部。也许有人可以告诉我如何获得正确的引用来加载和定义脚本。代码如下:

(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);
(函数(jQuery){
“严格使用”;
jQuery.fn.ysExit=函数(o){
var$self=这个;
var默认值={
宽度:“40%”,//弹出窗口宽度
高度:“30%”,//弹出高度
目标:“#ys容器”,//弹出选择器
有效期:1,//天
closeOnOutsideClick:true,//如果用户在外部单击,则关闭弹出窗口
延迟:0,//在注册弹出窗口之前的延迟(毫秒)
debug:false,//如果为true,则不会设置cookie
cookieName:“ysExit”
},
content=insertContent(),
选项=固定选项(o),
元素=document.querySelector(options.target),
层=document.querySelector(“.ys层”),
closeBt=document.querySelector(options.target+'.ys popup close'),
内部=document.querySelector(options.target+'.ys-box'),
exitBt=document.querySelector(options.target+'.ys exit'),
//cookies管理助手
cookies={
集合:函数(名称、值、天数){
变量组件=[name+'='+value];
如果(天){
变量日期=新日期();
date.setTime(date.getTime()+(天*24*3600*1000));
components.push('expires='+date.togmString());
}
push('path=/');
document.cookie=components.join(“;”);
},
get:函数(名称){
var start=name+'=',
arr=document.cookie.split(“;”),
我
对于(i=0;i<body>
  <div id="popup">...</div>
</body>
<body>
  <div class="some-class">
    <div id="popup">...</div>
  </div>
</body>
<div id="popup-demo"><!-- Direct `body` child -->
    <h3>Popup Demo</h3>
    <p>Content goes here...</p>
</div>

<script>
    jQuery('#popup-demo').ysExit({});
</script>