未终止的字符串文字

时间:2011-07-29 16:37:46

标签: javascript string uploadify

我有一个php脚本,它在我的服务器上传mp3文件。我使用'uploadify'。有一个事件'onSelect'(documentation),在上传文件时调用。使用它我想在每个文件上传后动态刷新播放列表。

但是我在'onselect'字段中的函数会抛出错误'unterminated string literal'。经过长时间的搜索,我将我的单行函数改为多行(为了方便起见,这里添加了分隔线)并在双引号之前添加斜杠。

function() {$('#response').append("
<script type=\"text/javascript\">
    var flashvars = {url:'./media/audio/users/126/md6xs4cv8ks0.mp3',artist:'Undef', track:'Undef', duration:''};
    var Params = {};
    var attributes = {};
    swfobject.embedSWF(\"min.swf\", \"myAlternativeContent1\", \"320\", \"40\", \"9.0.0\", false, flashvars, params, attributes);
</script>
<div id="myAlternativeContent1">
    <a href="http://www.adobe.com/go/getflashplayer">
    <img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash player" />
    </a>
</div>

3 个答案:

答案 0 :(得分:5)

与其他答案相反,字符串文字可以包含换行符,只需要使用反斜杠进行转义:

var string = "hello\
world!";

但是,这不会在字符串中创建换行符,因为它必须是显式的\n转义序列。从技术上讲,这将成为helloworld。做

var string = "hello"
+ "world"

会更清洁

答案 1 :(得分:2)

您帖子中突出显示的语法清楚地表明:您的<div>块中有未转义的引号(并且您忘记了脚本的结尾)。

无论如何,Javascript中的字符串文字不能跨越多行。

而不是:

alert("lol
       wtf");

写:

alert("lol" +
      "wtf");

您可能还会考虑拆分</script>子字符串which has been known to cause parsing problems

"</scr" + "ipt>"

(严格地说,为了相同,上面的第二个例子在\n之后会有一个lol和一些空格,但在编写HTML标记的情况下这并不重要。 )功能

答案 2 :(得分:0)

可能是</script>的存在。尝试转义一个或多个这些字符(即改为使用<\/script>

相关问题