gpt4 book ai didi

javascript - 延迟加载脚本

转载 作者:行者123 更新时间:2023-11-29 17:38:09 25 4
gpt4 key购买 nike

我正试图延迟在我的网站上加载此聊天脚本,但由于某种原因无法使其正常工作。

console.log(script) 返回正确动态生成的脚本,但由于某种原因脚本(实时聊天)未在页面上运行。

出于隐私考虑,我删除了一些内容,但这是我的代码:

HTML

<!DOCTYPE html>
<%@ LANGUAGE="VBSCRIPT" %>
<html lang="en-US">
<head>

<meta charset="utf-8">
<meta name="robots" content="noindex, nofollow">
<meta content="width=device-width, initial-scale=1, shrink-to-fit=no" name="viewport">
<meta name="description" content="Website Description">

<title>Website</title>

<link rel="alternate" href="https://www.website.com/website" hreflang="x-default"/>
<link rel="canonical" href="https://www.website.com/website">

<!--#INCLUDE VIRTUAL="/codeanalytics/google.htm"-->
<!--#INCLUDE VIRTUAL="/_borders17/assets/css-files.html"-->
<!--#INCLUDE VIRTUAL="/_borders17/website-2018-form-validation.html"-->


<%
FormHeader = "Request Demo Now"
FormBtn = "Get Demo Now"

sub Process
Interest = "Demo Request: Website - mrkt"

WebComment = Comments
CSR = UserLog(Interest, "DQT")
Message = Comments & vbCrLf
Message = Message & " Name: " & Name & vbCrLf
Message = Message & " Email: " & email & vbCrLf
Message = Message & " Phone: " & Phone & vbCrLf
Message = Message & " Company: " & Company & vbCrLf
Message = Message & " Product: " & Interest & " " & vbCrLf
Message = Message & " GDPR Agreement: " & Agreement & Checked & gdpragree & vbCrLf
Message = Message & " Referrer: " & REFERER & vbCrLf & vbCrLf
message = message + Ip2Message & vbCrLf & vbCrLf
result = Mail("email@email.com", emailfrom, Interest & " " & Reg & " " & IpCountry & " " & Csr, Message )
end sub
%>

</head>
<body>

<!--#INCLUDE VIRTUAL="/codeanalytics/google-tag-manager-body.htm"-->
<!--#INCLUDE VIRTUAL="/_borders17/nav/2018/nav.html"-->

<main>
<section>

<!-- HTML CONTENT HERE -->

</section>
</main>

<!--#INCLUDE VIRTUAL="/_borders17/footer/2018/footer.html"-->
<!--#INCLUDE VIRTUAL="/_borders17/assets/js-files.html"-->

<script>

setTimeout(function() {
var script = document.createElement('script');
script.type = "text/javascript";
script.setAttribute("id", "6461f488b9172537ed0bfce1966c165e");
script.src = "//support.website.com/script.php?id=6461f488b9172537ed0bfce1966c165e";
console.log(script);
document.querySelector('head').appendChild(script);
}, 5000);

</script>

</body>
</html>
<% sub Form%>
<%end sub%>

JS

    setTimeout(function() {
var script = document.createElement('script');
script.type = "text/javascript";
script.setAttribute("id", "6461f488b9172537ed0bfce1966c165e");
script.src = "//support.website.com/script.php?id=6461f488b9172537ed0bfce1966c165e";
console.log(script);
document.querySelector('head').appendChild(script);
}, 5000);

最佳答案

添加<head>在您的 html 中添加标签,它将起作用(运行并在 chrome 控制台中查看),并记住您的脚本应定义在该标签下方。

setTimeout(function() {
var script = document.createElement('script');
script.src = "//support.website.com/script.php?id=6461f488b9172537ed0bfce1966c165e";
script.type = "text/javascript";
script.setAttribute("id", "6461f488b9172537ed0bfce1966c165e");
document.getElementsByTagName('head')[0].appendChild(script);
}, 50);
<head>xxx</head>

问题更新后

我将您的完整代码(在问题更新后)放入代码片段中,它可以正常运行,您在 previous versions 中提到的错误你的问题

Uncaught TypeError: Cannot read property 'appendChild' of null.

这里是片段

<!DOCTYPE html>
<%@ LANGUAGE="VBSCRIPT" %>
<html lang="en-US">
<head>

<meta charset="utf-8">
<meta name="robots" content="noindex, nofollow">
<meta content="width=device-width, initial-scale=1, shrink-to-fit=no" name="viewport">
<meta name="description" content="Website Description">

<title>Website</title>

<link rel="alternate" href="https://www.website.com/website" hreflang="x-default"/>
<link rel="canonical" href="https://www.website.com/website">

<!--#INCLUDE VIRTUAL="/codeanalytics/google.htm"-->
<!--#INCLUDE VIRTUAL="/_borders17/assets/css-files.html"-->
<!--#INCLUDE VIRTUAL="/_borders17/website-2018-form-validation.html"-->


<%
FormHeader = "Request Demo Now"
FormBtn = "Get Demo Now"

sub Process
Interest = "Demo Request: Website - mrkt"

WebComment = Comments
CSR = UserLog(Interest, "DQT")
Message = Comments & vbCrLf
Message = Message & " Name: " & Name & vbCrLf
Message = Message & " Email: " & email & vbCrLf
Message = Message & " Phone: " & Phone & vbCrLf
Message = Message & " Company: " & Company & vbCrLf
Message = Message & " Product: " & Interest & " " & vbCrLf
Message = Message & " GDPR Agreement: " & Agreement & Checked & gdpragree & vbCrLf
Message = Message & " Referrer: " & REFERER & vbCrLf & vbCrLf
message = message + Ip2Message & vbCrLf & vbCrLf
result = Mail("email@email.com", emailfrom, Interest & " " & Reg & " " & IpCountry & " " & Csr, Message )
end sub
%>

</head>
<body>

<!--#INCLUDE VIRTUAL="/codeanalytics/google-tag-manager-body.htm"-->
<!--#INCLUDE VIRTUAL="/_borders17/nav/2018/nav.html"-->

<main>
<section>

<!-- HTML CONTENT HERE -->

</section>
</main>

<!--#INCLUDE VIRTUAL="/_borders17/footer/2018/footer.html"-->
<!--#INCLUDE VIRTUAL="/_borders17/assets/js-files.html"-->

<script>

setTimeout(function() {
console.log('xx');
var script = document.createElement('script');
script.type = "text/javascript";
script.setAttribute("id", "6461f488b9172537ed0bfce1966c165e");
script.src = "//support.website.com/script.php?id=6461f488b9172537ed0bfce1966c165e";
console.log(script);
document.querySelector('head').appendChild(script);
}, 5000);

</script>

</body>
</html>
<% sub Form%>
<%end sub%>

当您运行它并转到 chrome 元素检查器时,您会注意到您的脚本出现在片段 <head> 中。标记(我还将 console.log('xx') 添加到您的 js 以确保您的脚本已执行):

enter image description here

如果您出现错误停止(因为在这个版本的问题中您将其删除)并且实时聊天仍然无法正常工作,那么这是一个单独的问题并在 StackOverflow 中查找/提出另一个问题(将您的脚本也放在那里 //support.website.com/script.php?id=6... 内容)

关于javascript - 延迟加载脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54932468/

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