未终止的字符串文字/无效或意外的令牌 [英] unterminated string literal / Invalid or unexpected token

查看:44
本文介绍了未终止的字符串文字/无效或意外的令牌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为什么得到...

SyntaxError:未终止的字符串文字

SyntaxError: unterminated string literal

...在 Firefox 和...

未捕获的SyntaxError:无效或意外的令牌

Uncaught SyntaxError: Invalid or unexpected token

...在我运行时在 Chrome 中...

...in Chrome when I run...

$(document).ready(function () {
  function addJSBeforeEndBody(code) {
       $('body').append('<script>' + code + '</script>');
  }
  addJSBeforeEndBody('$(document).ready(function() { console.log("I never end up here."); });');
});

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

推荐答案

分解字符串</script>" (在javascript代码中);它将被解释为实际的结束脚本标签,而不是您想要的字符串文字.

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>

这篇关于未终止的字符串文字/无效或意外的令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆