unterminated string literal / Invalid or unexpected token

后端 未结 2 1837
無奈伤痛
無奈伤痛 2021-01-13 19:43

Why am I getting...

SyntaxError: unterminated string literal

...in Firefox and...

Uncaug

相关标签:
2条回答
  • 2021-01-13 19:52

    Break up the string "</script>" (in the javascript code); it will be interpreted as an actual closing script tag rather than the string literal you intend.

    $(document).ready(function () {
      function addJSBeforeEndBody(code) {
           $('body').append('<script>' + code + '</scr' + 'ipt>');
      }
      addJSBeforeEndBody('$(document).ready(function() { console.log("It works now."); });');
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

    0 讨论(0)
  • 2021-01-13 19:58
    '<script>'+code.toString()+'<\/script>'
    

    The browser closes the script element when it sees '</script>', indicating the script close tag. Escaping the slash keeps the browser in text interpreting mode.

    Or breaking it up as mentioned:

    "<script>"+code.toString()+"<"+"/script>"
    
    0 讨论(0)
提交回复
热议问题