简体   繁体   中英

unterminated string literal

I have a php-script, which uploads mp3-files on my server. I use 'uploadify' for it. There is an event 'onSelect'( documentation ), which called when file has been uploaded. Using it I want to refresh playlist dynamically after each file's uploading.

But my function from 'onselect' field throws an error 'unterminated string literal'. After long searching I make my one-line function instead multi-line(breaklines have been added here for the convenience) and add slashes before double quotes.

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>

Contrary to the other answers, string literals can contain line breaks, they just need to be escaped with a backslash like so:

var string = "hello\
world!";

However, this does not create a line break in the string, as it must be an explicit \\n escape sequence. This would technically become helloworld . Doing

var string = "hello"
+ "world"

would be much cleaner

The syntax highlighting right there in your post makes it clear: you have unescaped quotes in your <div> block (and you forgot the end of your script).

Anyway, string literals in Javascript can't span multiple lines.

Instead of:

alert("lol
       wtf");

write:

alert("lol" +
      "wtf");

You might also consider breaking apart the </script> substring, which has been known to cause parsing problems :

"</scr" + "ipt>"

(Strictly speaking, to be equivalent, the second example above would have a \\n and some spaces after lol , but that shouldn't matter in your case of writing HTML markup.)

Could be the presence of the </script> . Try escaping one or more of those characters (ie use <\\/script> instead)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM