按照this documentation的说明,我最终得到以下代码:
window.zEmbed || (function (d, s) {
var z = $zopim = function (c) { z._.push(c) },
$ = z.s =
d.createElement(s), e = d.getElementsByTagName(s)[0]; z.set = function (o) {
z.set.
_.push(o)
}; z._ = []; z.set._ = []; $.async = !0; $.setAttribute("charset", "utf-8");
$.onload = (event) => {
console.log("chat script has loaded");
};
$.src = "//v2.zopim.com/?@zopimClientID"; z.t = +new Date; $.
type = "text/javascript"; e.parentNode.insertBefore($, e);
})(document, "script");
var onChatStart = function () {
$zopim.livechat.window.show();
window['onChatStart'] && window['onChatStart']();
};
$zopim(function () {
$zopim.livechat.clearAll();
(......)
});
问题在于,从未调用$zopim(funtion() {..})
回调函数,但脚本已成功加载。我知道基于以下内容的console.log:
$.onload = (event) => {
console.log("chat script has loaded");
};
有人知道为什么会这样吗? 谢谢
答案 0 :(得分:0)
问题是您正在$zopim
脚本完全加载之前运行它。 $zopim
对象的所有部分加载完毕后,它将停止循环并运行您的脚本。
var zopimConfig = function() {
$zopim(function() {
$zopim.livechat.clearAll();
(......)
});
};
var waitForZopim = setInterval(function () {
if (
window.$zopim === undefined ||
window.$zopim.livechat === undefined ||
window.$zopim.livechat.departments === undefined
) {
return;
}
zopimConfig();
clearInterval(waitForZopim);
}, 100);
等待window.$zopim.livechat.departments
是可选的,但是如果您在部门周围做任何事情,我建议您保留此