Home > OS >  How do I fix comment showing on code in javascript?
How do I fix comment showing on code in javascript?

Time:03-13

Im trying to download jQuery into my document via my js file. when i do document.write however, the "https://" is marked as a comment.

function downloadjQuery() {
    document.open();
    document.write("<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>");
    document.close();
}

CodePudding user response:

In your code, you use four same quotes in string, so JavaScript thinks your thinks ends there:

"<script src="

I recommend use ' instead of double quotes, or you can use \' or \", also you can use `, some examples:

let Hello = "How 'are' you?"
let World = "How \"you\" did this?"
let User = `
Hey there!
`

So try this out:

function downloadjQuery() {
    document.open();
    document.write("<script src='https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js'></script>");
    document.close();
}

CodePudding user response:

You're using double quotes and single quotes wrong

You can try to escape double quotes with \"or use single quotes '.

This line is wrong

"<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>"

it gets read as "<script src=" https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js "></script>"

CodePudding user response:

Your quote use is incorrect. You closed the quotes before the link.

Instead of:

document.write("<script src="link"></script>");

You should do:

document.write("<script src='link'></script>");

When you have to use quotes multiple times, you can also use ' instead of " four times. Also, if you want to keep the raw version of the comment, you can type \ before //.

Example:

(Correct Usage)

\//I printed a raw version of a comment.

(Incorrect Usage)

//I did not print a raw version of a comment.

CodePudding user response:

Please try this

function downloadjQuery() {
    document.open();
    document.write('<script src="https:\//ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>');
    document.close();
}

CodePudding user response:

Do as the following,

<button onclick="myfx()">Download</button>

<script>
    function downloadjQuery(){
        var jQs = document.createElement("script");
        jQs.src = "https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js";
        document.body.appendChild(jQs);
    }
</script>

Hope it helped you, please upvote the answer :)

  • Related