如何将外部脚本包含到 <script> 中?

How to include external script into <script>?

在我的 JS 函数中,我需要添加:

< script src=https://xxxx.com/c3.js type="text/javascript" async defer>< /script>

如何正确编辑代码?

<script>
function affilate() {                   
        var ab_instance = "xxx.com";
        var ab_kampan = 3;
        var ab_cena = 1; 
        var ab_id_transakce = document.getElementById("email").value;
              
    <script src=https://xxxx.com/c3.js type="text/javascript" async defer></script>
                                    
    console.log(ab_id_transakce);
    }
 </script>

如果我对你的问题的理解正确,你是在尝试向 DOM 添加脚本标签。看来您正试图在 Javascript 函数中添加 HTML 脚本标记,这将不起作用。您可以尝试的是:

  1. 直接在 HTML 中添加脚本标签,如下所示:
<script src=https://xxxx.com/c3.js type="text/javascript" async defer></script>
<script>
function affilate() {                   
    var ab_instance = "xxx.com";
    var ab_kampan = 3;
    var ab_cena = 1; 
    var ab_id_transakce = document.getElementById("email").value;                             
    console.log(ab_id_transakce);
}
 </script>
  1. 动态添加脚本标签
<script>
    function affilate() {
        var ab_instance = "xxx.com";
        var ab_kampan = 3;
        var ab_cena = 1;
        var ab_id_transakce = document.getElementById("email").value;
        console.log(ab_id_transakce);

        var script_tag = document.createElement("script");
        script_tag.src = "https://xxxx.com/c3.js";
        script_tag.type = "text/javascript";
        script_tag.async = true;
        script_tag.defer = true;

        // in case you want to add the script tag to the <head>
        document.head.appendChild(script_tag);

        // in case you want to add the script tag to the <body>
        document.body.appendChild(script_tag);
    }
</script>