gpt4 book ai didi

javascript - 函数不会在页面加载时运行

转载 作者:行者123 更新时间:2023-12-03 07:52:04 25 4
gpt4 key购买 nike

我已经更改了在页面加载时加载的函数,但它不会这样做。

original code它通过链接运行:

function updateBannerText(text) {
smartsupp('banner:set', 'bubble');
smartsupp('banner:update', { text: text });
});

<a href="javascript:updateBannerText('Welcome')">Welcome</a>,
<a href="javascript:updateBannerText('This chat is awesome')">This chat is awesome</a>

我的代码应该在页面加载时运行:

function updateBannerText(text) {
smartsupp('banner:set', 'bubble');
smartsupp('banner:update', { text: "{$Artikel->cName}" });
}
window.onload = updateBannerText;

我已将 updateBannerText(text) 更改为 updateBannerText(),但无法运行它。

最佳答案

问题是 Smartsupp 小部件本身仅在页面加载时开始加载,因此您的 window.onload 函数在小部件正确初始化之前运行,因此没有效果。您可以通过延迟横幅更新来测试这一点,这应该会产生效果:

window.onload = function () { setTimeout(updateBannerText, 5000) }

要解决此问题,您应该将启动代码附加到适当的 Smartsupp widget event ,比如说 rendered event用于更新横幅:

smartsupp('on', 'rendered', function() {
updateBannerText('whatnot')
})

关于javascript - 函数不会在页面加载时运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34961850/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com