[英]How to I add code into <script> </script>
我正在嘗試將代碼添加到腳本中,但它不起作用
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);
這條路; 轉義雙引號(內部內容用"
括起來),反之亦然。另一種方法是使用數組使代碼可讀,並使用.join()
將它們連接起來
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);
使用數組:
var script= document.createElement('script'); scriptContent = ['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",', '};', 'console.log(Module)'].join(""); script.type= 'text/javascript'; script.textContent = scriptContent; script.async = true; document.body.appendChild(script);
只需這樣編寫代碼:
<script>
// your javascript code
</script>
或制作另一個JavaScript文件,例如。 “ main.js”,然后在正文末尾之前將其加載到您的HTML中:
<script src="location/of/main.js"></script>
現代瀏覽器允許
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",
};`;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.