unterminated string literal

前端 未结 9 1306
轮回少年
轮回少年 2020-11-30 06:07

The following code:

var str= \"English Comprehension<\\/strong>
                    
                    
相关标签:
9条回答
  • 2020-11-30 06:16

    My code was a response.write() where I needed to insert html to be outputted POST SUbmit on a form within an Iframe. When copying my code over from an text editor the carraige return carried over into the code window. Once I deleted my returns and whitespace between tags so that the code looked like a block paragraph that fixed the issue:

    ("loremIPSUM.<br><br><h2>Title</h2><br>loremIPSUM:<br><br><br><a href='http://url1'>lnk1</a><br><br><a href='http://url2'>loremIPSUMlnk2</a><br><br><a href='http://url3'>loremIPSUM</a><br><br><a href='url4'>IF THE case id is: "</a>"+value)
    
    0 讨论(0)
  • 2020-11-30 06:21

    A good way to concatenate strings in javascript is fallowing:

    var stringBuilder = [];
    
    stringBuilder.push("<strong>English Comprehension</strong>");
    stringBuilder.push("<br />");
    stringBuilder.push("<ul>");
    ...
    var resultString = stringBuilder.join("");
    

    This method faster than

    var str = "a"+"b" +"c"
    
    0 讨论(0)
  • 2020-11-30 06:23

    If using multiline string in JavaScript you must use '\' in the end of each line:

    var str = "abc\
    def\
    ghi\
    jkl";
    

    Also beware extra whitespace if your code is indented.

    0 讨论(0)
  • 2020-11-30 06:27

    I suggest to do it differently... have hidden element with that HTML e.g.

    <div id="myHiddenDiv" style="display: none;"><strong>English Comprehension</strong>
    <br />
    ...
    </div>
    

    Then simply read its inner HTML:

    var str = document.getElementById("myHiddenDiv").innerHTML;
    

    The big advantage is that you won't have to fight your way with literal strings and it's much easier to edit, the downside is that you add another element to the DOM. Your choice. :)

    0 讨论(0)
  • 2020-11-30 06:29

    You can't split a string across lines like that in javascript. You could accomplish the same readability by making each line a separate string and concatenating them with the plus sign like so:

    var str = "<strong>English Comprehension</strong>"
        + "<br />"
        + "<ul>"
        + "<li>Synonyms/Antonyms/Word Meaning (Vocabulary)</li>"
    

    and so on...

    0 讨论(0)
  • 2020-11-30 06:30

    It is not a best practice to write multiline strings in Javascript.

    But, you can do that with the \ terminator:

    var str= "<strong>English Comprehension<\/strong>\
                    <br\/>\
                    <ul>\
                    <li>  Synonyms/Antonyms/Word Meaning (Vocabulary)<\/li>\
                    <li>  Complete the Sentence (Grammar)<\/li>\
                    <li>  Spot error/Correct sentence (Grammar/sentence construction)<\/li>\
                    <li>  Sentence Ordering (Comprehension skills)<\/li>\
                    <li>  Questions based on passage (Comprehension skills)<\/li>\
                    <\/ul>\
                    <br\/>";
    

    Note that trailing spaces are part of the string, so it is better to get rid of them, unless they are intentional:

    var str= "<strong>English Comprehension<\/strong>\
    <br\/>\
    <ul>\
    <li>  Synonyms/Antonyms/Word Meaning (Vocabulary)<\/li>\
    <li>  Complete the Sentence (Grammar)<\/li>\
    <li>  Spot error/Correct sentence (Grammar/sentence construction)<\/li>\
    <li>  Sentence Ordering (Comprehension skills)<\/li>\
    <li>  Questions based on passage (Comprehension skills)<\/li>\
    <\/ul>\
    <br\/>";
    
    0 讨论(0)
提交回复
热议问题