3

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>
dark
  • 167
  • 1
  • 6
  • 17

3 Answers3

8

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

Richard Hoffman
  • 729
  • 3
  • 10
  • "String literals can contain line breaks" "This does not create a line break in the string". You're right about `\\`, but if you're going to word your answer in a way that tries to devalue other answers, you should be absolutely certain that yours is of the highest possible calibre. – Lightness Races in Orbit Aug 02 '11 at 20:46
  • I don't see what's wrong with my post. The string **literal** contains the line break. The **string** object that results from it does not. – Richard Hoffman Aug 02 '11 at 20:52
3

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.)

Community
  • 1
  • 1
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
0

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

Steve Wang
  • 1,814
  • 1
  • 16
  • 12