Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/423.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在按钮上使用onclick启动javascript_Javascript_Wordpress_Function_Onclick - Fatal编程技术网

在按钮上使用onclick启动javascript

在按钮上使用onclick启动javascript,javascript,wordpress,function,onclick,Javascript,Wordpress,Function,Onclick,大家好,我对使用函数和onclick操作调用javascript非常陌生,所以我可以借助一些帮助。基本上,我已经在WordPress上安装了一个插件,它以小部件的形式在页面上添加了一个按钮,一旦点击它就会启动一个脚本。但是,我不喜欢他们的按钮,所以我尝试自己编写代码,但我希望它能像那样启动脚本 以下是脚本代码: /* * Timely BookButton plugin * Example usage: * var button = new timelyButton('doedayspa');

大家好,我对使用函数和onclick操作调用javascript非常陌生,所以我可以借助一些帮助。基本上,我已经在WordPress上安装了一个插件,它以小部件的形式在页面上添加了一个按钮,一旦点击它就会启动一个脚本。但是,我不喜欢他们的按钮,所以我尝试自己编写代码,但我希望它能像那样启动脚本

以下是脚本代码:

/*
* Timely BookButton plugin
* Example usage:
* var button = new timelyButton('doedayspa');
*
* Booking process can be kicked off manually by calling the start method  on     the button instance e.g.
* button.start();
*
*/

// Need this for legacy support of older versions of the BookingButton
var timelyButton;

(function () {

"use strict";
var context = window;

var mobile = {
    Android: function () {
        return navigator.userAgent.match(/Android/i) ? true : false;
    },
    BlackBerry: function () {
        return navigator.userAgent.match(/BlackBerry/i) ? true : false;
    },
    iOS: function () {
        return navigator.userAgent.match(/iPhone|iPod/i) ? true : false;
    },
    Windows: function () {
        return navigator.userAgent.match(/IEMobile/i) ? true : false;
    },
    any: function () {
        return (mobile.Android() || mobile.BlackBerry() || mobile.iOS() || mobile.Windows());
    }
};


timelyButton = function (id, opts) {

    var options = opts || {};
    var businessId = id;
    var resellerCode = options.reseller || resellerCode || '';
    var productId = options.product || productId || '';
    var categoryId = options.category || categoryId || '';
    var staffId = options.staff || staffId || '';
    var locationId = options.location || locationId || '';
    var giftVoucherId = options.giftVoucherId || giftVoucherId || '';
    var isPurchaseButton = options.isPurchaseButton != null ? options.isPurchaseButton : false; // default not a purchase
    var dontCreateButton = !!options.dontCreateButton;
    window.timelyBookFrame = {};
    var XD;
    var style = options.style || 'light';
    var buttonId = options.buttonId || false;
    var bookButton;

    var scriptSource = (function() {
        var script = document.getElementById('timelyScript');
        if (script.getAttribute.length !== undefined) {
            return script.src;
        }
        return script.getAttribute('src', -1);
    }());

    var isOwnImage = !!options.imgSrc;
    var imgButtonType = isPurchaseButton ? "purchase-buttons" : "book-buttons";
    var imgSrc = options.imgSrc || getDomain() + '/images/' + imgButtonType + '/button_' + style + '@2x.png';
    var hoverSrc = getDomain() + '/images/' + imgButtonType + '/button_' + style + '_hover@2x.png';
    var activeSrc = getDomain() + '/images/' + imgButtonType + '/button_' + style + '_active@2x.png';
    var locationUrl = (isPurchaseButton ? '/giftvoucher/details/' : '/booking/location/') + businessId;

    function init() {
        if (dontCreateButton) return true;
        if (isOwnImage) {
            bookButton = document.createElement('a');
            bookButton.href = 'javascript:void(0)';
            bookButton.onclick = eventHandler.prototype.Book;
            bookButton.innerHTML = '<img src=\'' + imgSrc + '\' border=\'0\' />';
        } else {
            bookButton = document.createElement('a');
            bookButton.style.backgroundImage = "url(" + imgSrc + ")";
            bookButton.style.backgroundRepeat = "no-repeat";
            bookButton.style.backgroundPosition = "0px 0px";
            bookButton.style.backgroundSize = (isPurchaseButton ? "220px" : "162px") + " 40px";
            bookButton.style.width = isPurchaseButton ? "220px" : "162px";
            bookButton.style.height = "40px";
            bookButton.style.display = "inline-block";
            bookButton.href = 'javascript:void(0)';
            bookButton.onclick = eventHandler.prototype.Book;
            bookButton.innerHTML += '<img src="' + hoverSrc + '" style="display:none;" border=\'0\' />';
            bookButton.innerHTML += '<img src="' + activeSrc + '" style="display:none;" border=\'0\' />';
            bookButton.onmouseenter = function() { this.style.backgroundImage = "url(" + hoverSrc + ")"; };
            bookButton.onmouseout = function () { this.style.backgroundImage = "url(" + imgSrc + ")"; };
            bookButton.onmousedown = function () { this.style.backgroundImage = "url(" + activeSrc + ")"; };
            bookButton.onmouseup = function () { this.style.backgroundImage = "url(" + hoverSrc + ")"; };
        }
        var insertionPoint = findInsertionPoint(buttonId);
        insertAfter(bookButton, insertionPoint);
    }

    function findInsertionPoint(buttonId) {
        var insertionPoint = false;
        if (buttonId) {
            insertionPoint = document.getElementById(buttonId);
        } else {
            if (("currentScript" in document)) {
                insertionPoint = document.currentScript;
            } else {
                var scripts = document.getElementsByTagName('script');
                insertionPoint = scripts[scripts.length - 1];
            }
        }
        return insertionPoint;
    }

    function getDomain() {
        return ('https:' == document.location.protocol ? 'https://' : 'http://') + scriptSource.match( /:\/\/(.[^/]+)/ )[1];
    }

    function startBooking() {
        var url = "";
        if (resellerCode) {
            url += '&reseller=' + resellerCode;
        }
        if (productId) {
            url += '&productId=' + productId;
        }
        if (categoryId) {
            url += '&categoryId=' + categoryId;
        }
        if (staffId) {
            url += '&staffId=' + staffId;
        }
        if (locationId) {
            url += '&locationId=' + locationId;
        }
        if (giftVoucherId) {
            url += '&giftVoucherId=' + giftVoucherId;
        }

        if (window.innerWidth < 768 || mobile.any()) {
            url = getDomain() + locationUrl + "?mobile=true" + url;
            window.location.href = url;
            return;
        }
        window.timelyBookFrame = document.createElement('iframe');
        window.timelyBookFrame.className = 'timely-book-frame';
        window.timelyBookFrame.style.cssText = 'width: 100%; height: 100%; position: fixed; top: 0; left: 0; z-index: 99999999;';
        window.timelyBookFrame.setAttribute('frameBorder', 0);
        window.timelyBookFrame.setAttribute('allowTransparency', 'true');
        url = getDomain() + (isPurchaseButton ? '/giftvoucher' : '/booking') + '/overlay/' + businessId + '?x' + url;
        url += '#' + encodeURIComponent(document.location.href);
        window.timelyBookFrame.src = url;
        window.timelyBookFrame.style.display = 'none';
        document.getElementsByTagName('body')[0].appendChild(window.timelyBookFrame);
        var element = document.getElementById('timely-lightbox');
        if (typeof(element) != 'undefined' && element != null) {
            $('#timely-lightbox').fadeOut();
        }
    }

    function insertAfter(f, n) {
        var p = n.parentNode;
        if (n.nextSibling) {
            p.insertBefore(f, n.nextSibling);
        } else {
            p.appendChild(f);
        }
    }

    function eventHandler() {
        // prototype instance
    }

    eventHandler.prototype.Book = function() {
        startBooking();
    };

    // everything is wrapped in the XD function to reduce namespace collisions
    XD = function () {

        var interval_id,
            last_hash,
            cache_bust = 1,
            attached_callback,
            window = context;

        return {
            postMessage: function (message, target_url, target) {
                if (!target_url) {
                    return;
                }
                target = target || parent; // default to parent
                if (window['postMessage']) {
                    // the browser supports window.postMessage, so call it with a targetOrigin
                    // set appropriately, based on the target_url parameter.
                    target['postMessage'](message, target_url.replace(/([^:]+:\/\/[^\/]+).*/, '$1'));
                } else if (target_url) {
                    // the browser does not support window.postMessage, so use the window.location.hash fragment hack
                    target.location = target_url.replace(/#.*$/, '') + '#' + (+new Date) + (cache_bust++) + '&' + message;
                }
            },
            receiveMessage: function (callback, source_origin) {
                // browser supports window.postMessage
                if (window['postMessage']) {
                    // bind the callback to the actual event associated with window.postMessage
                    if (callback) {
                        attached_callback = function (e) {
                            if ((typeof source_origin === 'string' && e.origin !== source_origin)
                                || (Object.prototype.toString.call(source_origin) === "[object Function]" && source_origin(e.origin) === !1)) {
                                return !1;
                            }
                            callback(e);
                        };
                    }
                    if (window['addEventListener']) {
                        window[callback ? 'addEventListener' : 'removeEventListener']('message', attached_callback, !1);
                    } else {
                        window[callback ? 'attachEvent' : 'detachEvent']('onmessage', attached_callback);
                    }
                } else {
                    // a polling loop is started & callback is called whenever the location.hash changes
                    interval_id && clearInterval(interval_id);
                    interval_id = null;
                    if (callback) {
                        interval_id = setInterval(function () {
                            var hash = document.location.hash,
                                re = /^#?\d+&/;
                            if (hash !== last_hash && re.test(hash)) {
                                last_hash = hash;
                                callback({ data: hash.replace(re, '') });
                            }
                        }, 100);
                    }
                }
            }
        };
    }();

    // setup a callback to handle the dispatched MessageEvent. if window.postMessage is supported the passed
    // event will have .data, .origin and .source properties. otherwise, it will only have the .data property.
    XD.receiveMessage(function (message) {

        if (message.data == 'close') {
            var element = document.getElementById('timely-lightbox');
            if (typeof (element) != 'undefined' && element != null) {
                $('#timely-lightbox').show();
            }
            if (window.timelyBookFrame && window.timelyBookFrame.parentNode) window.timelyBookFrame.parentNode.removeChild(window.timelyBookFrame);
        }
        if (message.data == 'open' && window.timelyBookFrame) {
            window.timelyBookFrame.style.display = 'block';
        }
    }, getDomain());

    init();

    // expose the BookButton API
    return {
        start: function() {
            startBooking();
        }
    };
};

})();
/*
*即时BookButton插件
*用法示例:
*var按钮=新的timelyButton('doedayspa');
*
*可以通过调用按钮实例上的start方法手动启动预订过程,例如。
*按钮。开始();
*
*/
//旧版本BookingButton的旧版支持需要此功能
var timelyButton;
(功能(){
“严格使用”;
var上下文=窗口;
移动增值={
Android:function(){
返回navigator.userAgent.match(/Android/i)?true:false;
},
黑莓:功能(){
返回navigator.userAgent.match(/BlackBerry/i)?true:false;
},
iOS:函数(){
返回navigator.userAgent.match(/iPhone | iPod/i)?true:false;
},
Windows:函数(){
返回navigator.userAgent.match(/IEMobile/i)?true:false;
},
any:函数(){
return(mobile.Android()| mobile.BlackBerry()| mobile.iOS()| mobile.Windows());
}
};
timelyButton=函数(id,选项){
var options=opts | |{};
var businessId=id;
var RELESSERCODE=options.RELESSERER | | RELESSERCODE | |“”;
var productId=options.product | | productId | |“”;
var categoryId=options.category | | categoryId | |'';
var staffId=options.staff | | staffId | |'';
var locationId=options.location | | locationId | |'';
var giftVoucherId=options.giftVoucherId | | giftVoucherId | |'';
var isPurchaseButton=options.isPurchaseButton!=null?options.isPurchaseButton:false;//默认不是购买
var dontCreateButton=!!options.dontCreateButton;
window.timelyBookFrame={};
var-XD;
var style=options.style | |“light”;
var buttonId=options.buttonId | | false;
var bookButton;
var scriptSource=(函数(){
var script=document.getElementById('timelyScript');
if(script.getAttribute.length!==未定义){
返回script.src;
}
返回script.getAttribute('src',-1);
}());
var isOwnImage=!!options.imgSrc;
var imgButtonType=isPurchaseButton?“采购按钮”:“书籍按钮”;
var imgSrc=options.imgSrc | | getDomain()++'/images/'+imgButtonType++'/button|style++'@2x.png';
var hoverSrc=getDomain()++'/images/'+imgButtonType++'/button\'+style++_hover@2x.png';
var activeSrc=getDomain()++'/images/'+imgButtonType++'/button\'+style++_active@2x.png';
var locationUrl=(isPurchaseButton?'/giftvoucher/details/':'/booking/location/)+businessId;
函数init(){
如果(dontCreateButton)返回true;
if(isOwnImage){
bookButton=document.createElement('a');
bookButton.href='javascript:void(0)';
bookButton.onclick=eventHandler.prototype.Book;
bookButton.innerHTML='';
}否则{
bookButton=document.createElement('a');
bookButton.style.backgroundImage=“url(“+imgSrc+”);
bookButton.style.backgroundRepeat=“无重复”;
bookButton.style.backgroundPosition=“0px 0px”;
bookButton.style.backgroundSize=(iPurchaseButton?“220px”:“162px”)+“40px”;
bookButton.style.width=isPurchaseButton?“220px”:“162px”;
bookButton.style.height=“40px”;
bookButton.style.display=“内联块”;
bookButton.href='javascript:void(0)';
bookButton.onclick=eventHandler.prototype.Book;
bookButton.innerHTML+='';
bookButton.innerHTML+='';
bookButton.onmouseinter=function(){this.style.backgroundImage=“url(“+hoverSrc+”);};
bookButton.onmouseout=函数(){this.style.backgroundImage=“url(“+imgSrc+”);};
bookButton.onmousedown=函数(){this.style.backgroundImage=“url(“+activeSrc+”);};
bookButton.onmouseup=function(){this.style.backgroundImage=“url(“+hoverSrc+”);};
}
变量插入点=findInsertionPoint(buttonId);
insertAfter(bookButton,insertionPoint);
}
函数findInsertionPoint(buttonId){
var insertionPoint=false;
如果(按钮){
insertionPoint=document.getElementById(buttonId);
}否则{
如果((文档中的“当前脚本”){
insertionPoint=document.currentScript;
}否则{
var scripts=document.getElementsByTagName('script');
insertionPoint=scripts[scripts.length-1];
}
}
返回插入点;
}
函数getDomain(){
return('https:'==document.location.protocol?'https://':'http://')+scriptSource.match(/:\/\/([^/]+)/)[1];
}
函数startBooking(){
var url=“”;
if(经销商代码){
url+='&经销商='+经销商代码;
}
if(productId){
url+='&productId='+productId;
}
if(类别ID){
url+='&categoryId='+categoryId;
}
如果(斯塔夫){
url+='&staffId='+staffId;
}
if(位置ID){
url+='&locationId='+locationId;
}
if(giftVoucherId){
url+='&giftVoucherId='+giftVoucherId;
}
if(window.innerWidth<768 | | mobile.any()){
url=getDomain()+locationUrl+“?mobile=true”+url;
window.location.href=url;
返回;
}
window.timelyBookFrame=document.createElement('iframe'