0

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

3 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

Comments

0

Just write your code like this:

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

Or make another JavaScript file, eg. "main.js", and then load it in your HTML right before the end of body:

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

Comments

0

Modern browsers allow

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",
};`;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.