延迟加载脚本

时间:2019-02-28 18:56:55

标签: javascript

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

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);

3 个答案:

答案 0 :(得分:2)

在您的html中添加<head>标签,它将起作用(在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中提到的错误均无误

  

未捕获的TypeError:无法读取null的属性'appendChild'。

这是摘要

<!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>标记中(我还向您的js添加了console.log('xx')以确保您的脚本已执行):

enter image description here

如果出现错误停止(因为在此问题的此版本中将其删除)并且仍然无法进行实时聊天,则这是单独的问题,请在StackOverflow中查找/询问另一个问题(在脚本{{1}中也放那里}内容)

答案 1 :(得分:1)

如果您要访问一个元素,请使用document.querySelector()。这样,您不必按索引访问头。另外,除非您此时必须导入脚本,否则通常最好在文档底部附近添加脚本。

这应该有效:

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.querySelector('head').appendChild(script);
}, 5000);
<head></head>

另外,请确保存在<head/>标签。

我的VBScript技能不是很好,但是请尝试在脚本中添加如下内容:

<script type="text/vbscript">
    Sub mySub
       Set script = document.createElement('script');
       script.src = "//support.website.com/script.php?id=6461f488b9172537ed0bfce1966c165e";
       script.type = "text/javascript";
       script.setAttribute("id", "6461f488b9172537ed0bfce1966c165e");
       document.querySelector('head').appendChild(script);
    End Sub
    window.setTimeout "mySub()", 50000, "VBScript"
</script>

希望这会有所帮助,

答案 2 :(得分:1)

在您的正文结尾处(在body结束标记之前)加载或运行脚本。