Problem with quotes in javascript output

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
This is not a javascript question ok?
First, the desired look of my endeavor is here.

What I am working on is here.

The problem mainly deals with required quotes in the javascript output.
Either single or double.

The code below shows you what I have been trying.
The output code shows the text with the variable names, not the desired

Do not run the code locally as the key will prevent it.

<!DOCTYPE html>
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
<script src="jwplayer/jwplayer.js"></script>

<div id='my-video'></div>
<script type='text/javascript'>

    playlist: [{
/* connect to the db */
$con = mysql_connect('localhost','user','pass');

if (!$con){die("can not connect: " . mysql_error());}



$result = mysql_query("SELECT atitle,artist,avid FROM A$year WHERE id =
if (!$result) { echo 'Could not run query: ' . mysql_error();  exit; }
$vid = mysql_fetch_row($result);

echo "{";

echo 'file: "
echo ",";
echo 'description: "$year # $number - $vid[1]"';

echo "}";


        // adjust these 2 lines if you'd like to omit/resize playlist, etc:


Re: Problem with quotes in javascript output

On 5/20/2013 12:17 PM, richard wrote:
Quoted text here. Click to load it

There is no variable substitution in single quoted ('...') strings, only  
double quoted ("...") strings.

You are using single quoted strings in your PHP code.

Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.

Re: Problem with quotes in javascript output


The problem with the player not showing was an extra {.
so now the script works just fine.

Re: Problem with quotes in javascript output

On Mon, 20 May 2013 12:17:26 -0400, richard wrote:

Quoted text here. Click to load it

Actually it is, because the problem seems to be that you're producing  
invalid javascipt.

Quoted text here. Click to load it

I don't think it does, although I suspect they may be an issue.

As far as I can tell, the final argument to jwplayer("my-video").setup()  
looks like this (I've added some spaces for readability):

{ playlist : [ { { file : "
description : "$year # $number - $vid[1]" } ], "startparam" : "start",  
"repeat" : "list", "width" : 800, listbar : { position : 'right', size :  
320}, "height" : 450 }

There are several things here that might cause problems:

1) Mismatched brackets. You don't have the same number of all of (, { and  
[ as you do respectively of ), } and ]. This is probably breaking the  

2) Some attributes are quoted and others are not. Although javascript is  
fairly tolerant as to which quotes you use in that it generally treats  
single and double quoted strings identically, I suspect that either you  
should be passing either strings for all the attributes, or names for all  
the attributes, but not a mixture of strings and names. For example,  
playlist, file, description, listbar, position and size are names; and  
"startparam", "repeat", "width" and "height" are strings. This may be  
breaking the javascript.

3) Your php variable substitution isn't going to work for two reasons.  
You need something more like:

echo "file: '
echo ",";
echo "description: '$year # $number - '";


echo "file: \"
echo ",";
echo "description: \"$year # $number - \"";

This is to address that (a) php variable substitution only works in  
double quoted strings (ie when using "" and not ''), and (b) you should  
wrap constructs of $array[element] with {} when using variable  
substitution. This is probably causing the data passed to the javascript  
to not be what you think it's getting.

Denis McMahon,

Re: Problem with quotes in javascript output

Denis McMahon wrote:

Quoted text here. Click to load it

ACK.  ?{ { ? } }? is not a valid Object initializer; the property name
?{foo: { ? } }?) is missing.

Quoted text here. Click to load it

_property names_

Quoted text here. Click to load it

? non-existent, see < , but ECMAScript  
implementations are (per Specification) ?

Quoted text here. Click to load it

It does not; quotes have always been optional for property names that are  
not reserved words and not numeric, and are completely optional since  
ECMAScript Edition 5 (2009).  However, if you are not sure whether a  
property name could be a reserved word or numeric, you should quote it for  
backwards compatibility.

Quoted text here. Click to load it

It would be best if they skipped the consecutive ?echo? altogether (almost  
always a bad idea to write such code; a mark of beginners) and let the  
preprocessor only work on what it has to.  That is, (in PHP 5.4)

<?php ? ?>
  file: "
=<?= addslashes($vid[2]) ?>",
  description: "<?= addslashes("$year # $number - ") ?>"
<?php ? ?>

In fact, it is even easier to json_encode() a PHP array as long as the leaf  
values can be expressed as ECMAScript primitive (JSON-compatible) values:

<?php ? ?>
  foo(<?= json_encode(array(
    'file':        "
    'description': "$year # $number - "
  )) ?>);
<?php ? ?>

(The leading and trailing PHP sections are optional.)

    realism:    HTML 4.01 Strict
    evangelism: XHTML 1.0 Strict
    madness:    XHTML 1.1 as application/xhtml+xml
                                                    -- Bjoern Hoehrmann

Site Timeline