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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14256320/
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-08-24 16:11:14  来源:igfitidea点击:

javascript Syntax error : Unterminated string literal

javascriptjquery

提问by DeDav

Possible Duplicate:
How do I break a string across more than one line of code in JavaScript?

可能的重复:
如何在 JavaScript 中的多行代码中拆分字符串?

I am getting an unterminated string literal error. Please see my code

我收到一个未终止的字符串文字错误。请看我的代码

<script type="text/javascript">
function embedVideo(url){
alert(video);
var video= '
<OBJECT ID="player" width="800" height="450" CLASSID="CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6" STANDBY="Loading Windows Media Player components..." TYPE="application/x-oleobject">
<PARAM NAME="FileName" VALUE="">
<PARAM name="autostart" VALUE="false">
<PARAM name="ShowControls" VALUE="true">
<param name="ShowStatusBar" value="true">
<PARAM name="ShowDisplay" VALUE="false">
<param name="uiMode" value="full" />
<PARAM NAME="SendPlayStateChangeEvents" VALUE="True" />
<EMBED TYPE="application/x-mplayer2" width="800" height="450" SRC="" NAME="MediaPlayer"
ShowControls="1" displaysize="4" ShowStatusBar="1" ShowDisplay="0" autostart="0"> </EMBED></OBJECT>
';
alert(video);
jQuery("#videoScreen").html(video);
return true;
}
</script>

Please help...

请帮忙...

回答by Alnitak

Javascript doesn't support multi-line strings, you'll need to either:

Javascript 不支持多行字符串,您需要:

  • make that tag one big line, or
  • use a trailing backslash on each line to indicate "continuation", or
  • use multiple strings joined together.
  • 把那个标签写成一条大线,或者
  • 在每一行使用尾随反斜杠来表示“继续”,或
  • 使用多个连接在一起的字符串。

回答by Pupil

I have copied and pasted the code into my machine. And I found the error.

我已将代码复制并粘贴到我的机器中。我发现了错误。

On line 4,

在第 4 行,

var video= '
<OBJECT ID="player" width="800" height="450" CLASSID="CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6" STANDBY="Loading Windows Media Player components..." TYPE="application/x-oleobject">

This is causing the error. You have put a line break on line 4 after the equal to =sign. Javascript considers every line break as new statement. Please remove unnecessary line breaks and it should work.

这是导致错误的原因。您在第 4 行等号后放置了一个换行符=。Javascript 将每个换行符视为新语句。请删除不必要的换行符,它应该可以工作。

Thanks.

谢谢。

回答by k102

You can't assign multiline values to a variable in js, so you have to write it this way:

你不能在 js 中给变量赋值多行值,所以你必须这样写:

var video = '<OBJECT ...>';
video += '...';

But... the way you're creating an objectelement is not correct - you'd better use document.createElementfor more clear code.

但是...您创建object元素的方式不正确 - 您最好使用document.createElement更清晰的代码。