首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >使用onclick按钮启动javascript

使用onclick按钮启动javascript
EN

Stack Overflow用户
提问于 2018-07-05 05:46:52
回答 1查看 334关注 0票数 0

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

下面是脚本代码:

代码语言:javascript
复制
/*
* 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();
        }
    };
};

})();

那么,当我单击按钮时,如何运行这个javascript呢?

任何帮助都将不胜感激!

EN

回答 1

Stack Overflow用户

发布于 2018-07-05 06:12:37

如果单击了该按钮,则可以自动创建脚本标记。

代码语言:javascript
复制
document.getElementById('myButton').addEventListener('click', () => {
	const script = document.createElement("script");
	script.src = 'my-other-file.js';
	document.head.appendChild(script);
})
代码语言:javascript
复制
<button id="myButton">Load JS File</button>

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51181085

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档