phongyewtong phongyewtong - 4 months ago 8
HTML Question

How to I add code into <script> </script>

I am trying to add code into script but it just doesnt work

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


How do i add to become like this?
enter image description here

Answer

This way; escaping the double quotes (enclose the inner content with ") and vice versa. Another way is to use an array to make your code readable and join them using .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);

Using array:

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