语法错误:未终止的字符串文字 javascript

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19790477/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 16:48:24  来源:igfitidea点击:

SyntaxError: unterminated string literal javascript

javascriptjquery

提问by MChan

I am getting a JavaScript error in my browser whenever I am trying to add the following two lines in the section of my application home page:

每当我尝试在应用程序主页的部分中添加以下两行时,我的浏览器都会出现 JavaScript 错误:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
    <script>window.jQuery || document.write('<script src="<%=request.getContextPath()%>/resources/jq/jquery-1.10.2.min.js"></script>')</script>

Can someone please tell me what is wrong in those two lines? and if possible how to fix it?

有人可以告诉我这两行有什么问题吗?如果可能的话如何解决它?

Thanks for your time

谢谢你的时间

回答by Pointy

You can't embed the substring </script>anywhere within a script block.

您不能</script>在脚本块内的任何位置嵌入子字符串。

Change your document.writecall:

更改您的document.write电话:

<script>
  window.jQuery || 
  document.write('<script src="<%=request.getContextPath()%>/resources/jq/jquery-1.10.2.min.js"></' + 'script>')
</script>

The browser "parses" <script>tag contents by blindly searching for a closing </script>tag, paying no attention whatsoever to the syntax of the contents.

浏览器<script>通过盲目搜索结束</script>标签来“解析”标签内容,完全不注意内容的语法。