我正在写一个 jquery 插件。我有一个为 LivePerson.com 动态添加的脚本标签。该脚本一旦加载,就应该触发配置中定义的 onLoad 函数。但是,这永远不会触发。我很好奇它是否可能是时间问题,因为插件是在准备好文档时启动的,并且在窗口加载时添加了脚本?有任何想法吗?
jQuery(function(){
jQuery(document).easyChat({
popup: false,
siteId:'XXXXXXXXX',
appKey: 'XXXXXXXXX'
});
});
插件脚本:
;(function($, document, window, undefined) {
// Optional, but considered best practice by some
"use strict";
// Name the plugin so it's only in one place
var pluginName = 'easyChat';
var lpc;
// Default options for the plugin as a simple object
var defaults = {
popup : true,
skill: 'Test'
};
// Plugin constructor
// This is the boilerplate to set up the plugin to keep our actual logic in one place
function Plugin(element, options) {
this.element = element;
// Merge the options given by the user with the defaults
this.options = $.extend({}, defaults, options)
// Initialization code to get the ball rolling
// If your plugin is simple, this may not be necessary and
// you could place your implementation here
if(this.options.popup != true){
this.init();
} else {
this.popup();
}
}
Plugin.prototype = {
// Public functions accessible to users
// Prototype methods are shared across all elements
// You have access to this.options and this.element
popup:function(){
console.log("popup");
},
DoAChat:function(){
lpc = new lpChat();
lpc.chatAvailability();
// alert('Before');
// lpc.requestChat();
// alert('After');
if(this.options.popup == true){
} else {
// document.getElementById('status').innerHTML += ' Done!';
}
},
myOnLoad:function() {
console.log("LP JS API Initiated");
this.DoAChat();
},
myOnInit:function() {
},
myOnStart:function() {
},
myOnStop:function() {
},
myOnState:function() {
},
myOnTyping:function() {
},
myOnPush:function() {
},
myOnLine:function() {
},
myOnError:function() {
},
myOnAvailability:function(availObj) {
console.log(availObj);
},
myOnResume:function() {
},
myOnAccountToAccountTransfer:function() {
},
//First Method triggered from plugin init
init: function() {
var lpChatConfig = {
apiKey : this.options.appKey,lpNumber : this.options.siteId,
lpServer: 'dev.liveperson.net',
onLoad : this.myOnLoad,
onInit : this.myOnInit,
onStart : this.myOnStart,
onStop : this.myOnStop,
onState : this.myOnState,
onAgentTyping : this.myOnTyping,
onUrlPush : this.myOnPush,
onLine : this.myOnLine,
onError : this.myOnError,
onAvailability: this.myOnAvailability,
onResume : this.myOnResume,
onAccountToAccountTransfer: this.myOnAccountToAccountTransfer,
skill: this.options.skill
};
lpChatConfig.lpAddScript = function(src, ignore) {var c = lpChatConfig;if(typeof(c.lpProtocol)=='undefined'){c.lpProtocol = (document.location.toString().indexOf("https:")==0) ? "https" : "http";}if (typeof(src) == 'undefined' || typeof(src) == 'object') {src = c.lpChatSrc ? c.lpChatSrc : '/hcp/html/lpChatAPI.js';};if (src.indexOf('http') != 0) {src = c.lpProtocol + "://" + c.lpServer + src + '?site=' + c.lpNumber;} else {if (src.indexOf('site=') < 0) {if (src.indexOf('?') < 0)src = src + '?'; else src = src + '&';src = src + 'site=' + c.lpNumber;}};var s = document.createElement('script');s.setAttribute('type', 'text/javascript');s.setAttribute('charset', 'iso-8859-1');s.setAttribute('src', src);document.getElementsByTagName('head').item(0).appendChild(s);}
if (window.attachEvent) window.attachEvent('onload', lpChatConfig.lpAddScript);
else window.addEventListener('load', lpChatConfig.lpAddScript, false);
}
};
$.fn[pluginName] = function(options) {
// Iterate through each DOM element and return it
return this.each(function() {
// prevent multiple instantiations
if (!$.data(this, 'plugin_' + pluginName)) {
$.data(this, 'plugin_' + pluginName, new Plugin(this, options));
}
});
};
// Private function that is only called by the plugin
var privateFunction = function() {
// ...
}
})(jQuery, document, window);