Search code examples
javascriptstringuploadify

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>

Solution

  • 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