如何在<script> </script>中添加代码

时间:2016-08-03 09:05:32

标签: javascript html

我正在尝试将代码添加到脚本中,但它只是不起作用

    var script= document.createElement('script');
    script.type= 'text/javascript';
    script.textContent = var Module = {
        TOTAL_MEMORY: 536870912,
        errorhandler: null, 
        compatibilitycheck: null,
        dataUrl: "Release/Arctopia_Path_Monopoly_v1.1.1GL.data",
        codeUrl: "Release/Arctopia_Path_Monopoly_v1.1.1GL.js",
        memUrl: "Release/Arctopia_Path_Monopoly_v1.1.1GL.mem",
    };
    script.async = true;
    document.body.appendChild(script);

我如何添加变成这样? enter image description here

3 个答案:

答案 0 :(得分:1)

这样;转义双引号(用function sendBuffer(data) { var length = Buffer.byteLength(data)+32; var headBuf = new Buffer(4); headBuf.writeUInt32BE(length); var bodyBuf = new Buffer(length); headBuf.copy(bodyBuf,0,0,headBuf.length); bodyBuf.write(data, headBuf.length+12); console.log(bodyBuf); client.write(bodyBuf);} 括起内部内容),反之亦然。另一种方法是使用数组使代码可读并使用"加入它们

&#13;
&#13;
.join()
&#13;
&#13;
&#13;

使用数组:

&#13;
&#13;
    var script= document.createElement('script');
    script.type= 'text/javascript';
    script.textContent = 'var Module = { TOTAL_MEMORY: 536870912, errorhandler: null, compatibilitycheck: null, dataUrl: "Release/Arctopia_Path_Monopoly_v1.1.1GL.data", codeUrl: "Release/Arctopia_Path_Monopoly_v1.1.1GL.js", memUrl: "Release/Arctopia_Path_Monopoly_v1.1.1GL.mem", };';
    script.async = true;
    document.body.appendChild(script);
&#13;
&#13;
&#13;

答案 1 :(得分:0)

只需编写如下代码:

<script>
  // your javascript code
</script>

或制作另一个JavaScript文件,例如。 &#34; main.js&#34;,然后在正文结束前将其加载到HTML中:

<script src="location/of/main.js"></script>

答案 2 :(得分:0)

现代浏览器允许

script.textContent = `var Module = {
    TOTAL_MEMORY: 536870912,
    errorhandler: null, 
    compatibilitycheck: null,
    dataUrl: "Release/Arctopia_Path_Monopoly_v1.1.1GL.data",
    codeUrl: "Release/Arctopia_Path_Monopoly_v1.1.1GL.js",
    memUrl: "Release/Arctopia_Path_Monopoly_v1.1.1GL.mem",
};`;