[英]How to include script inside javascript code?
I want to implement the following code to improve my site performance and try to fire the script after scroll and wait 1 second.我想实现以下代码来提高我的网站性能,并尝试在滚动后触发脚本并等待 1 秒。
<script type="text/javascript">
window.addEventListener('scroll',() =>
setTimeout(() => {
//Delay calendly
<script type="text/javascript" src="https://assets.calendly.com/assets/external/widget.js"></script>
}, 1000);
);
</script>
I have this calendly script that I want to delay.我有这个我想延迟的日历脚本。 The thing is I can't include the script inside another script, so, are there any way to make it?
问题是我不能将脚本包含在另一个脚本中,那么,有什么办法可以做到吗?
Thanks!谢谢!
You can do this with only the vanilla dom:您可以仅使用香草 dom 执行此操作:
// ... something happens
var scr_elem = document.createElement('script');
scr_elem.type = 'text/javascript'
scr_elem.src = 'https://assets.calendly.com/assets/external/widget.js'
document.body.appendChild(scr_elem);
You can create a script dynamically
and append to the head/body
.您可以
dynamically
创建脚本并将 append 到head/body
。
let timer;
window.addEventListener("scroll", () => {
if (!timer) {
timer = setTimeout(() => {
const script = document.createElement("script");
script.type = "text/javascript";
script.src = "https://assets.calendly.com/assets/external/widget.js";
document.body.appendChild(script);
}, 1000);
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.