Problem with quotes in javascript output [message #181527] |
Mon, 20 May 2013 16:17 |
|
richard
Messages: 213 Registered: June 2013
Karma: 0
|
Senior Member |
|
|
This is not a javascript question ok?
First, the desired look of my endeavor is here.
www.mroldies.net/test1.html
What I am working on is here.
www.mroldies.net/test2.php
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
output.
Do not run the code locally as the key will prevent it.
<!DOCTYPE html>
<html><head>
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
<script src="jwplayer/jwplayer.js"></script>
<script>jwplayer.key="NIV";</script>
</head>
<body>
<div id='my-video'></div>
<script type='text/javascript'>
jwplayer("my-video").setup({
playlist: [{
<?php
/* connect to the db */
$con = mysql_connect('localhost','user','pass');
if (!$con){die("can not connect: " . mysql_error());}
mysql_select_db('richbull_top100',$con);
$year=1960;
$number=1;
$result = mysql_query("SELECT atitle,artist,avid FROM A$year WHERE id =
$number");
if (!$result) { echo 'Could not run query: ' . mysql_error(); exit; }
$vid = mysql_fetch_row($result);
echo "{";
echo 'file: "http://www.youtube.com/watch?v=$vid[2]"';
echo ",";
echo 'description: "$year # $number - $vid[1]"';
echo "}";
?>
],
"startparam":"start",
"repeat":"list",
"width":800,
// adjust these 2 lines if you'd like to omit/resize playlist, etc:
listbar:{position:'right',size:320},
"height":450
});
</script>
</body>
</html>
|
|
|
Re: Problem with quotes in javascript output [message #181528 is a reply to message #181527] |
Mon, 20 May 2013 16:53 |
Jerry Stuckle
Messages: 2598 Registered: September 2010
Karma: 0
|
Senior Member |
|
|
On 5/20/2013 12:17 PM, richard wrote:
> This is not a javascript question ok?
> First, the desired look of my endeavor is here.
> www.mroldies.net/test1.html
>
> What I am working on is here.
> www.mroldies.net/test2.php
>
> 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
> output.
>
> Do not run the code locally as the key will prevent it.
>
>
>
> <!DOCTYPE html>
> <html><head>
> <meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
> <script src="jwplayer/jwplayer.js"></script>
> <script>jwplayer.key="NIV";</script>
> </head>
>
> <body>
> <div id='my-video'></div>
> <script type='text/javascript'>
>
> jwplayer("my-video").setup({
> playlist: [{
> <?php
> /* connect to the db */
> $con = mysql_connect('localhost','user','pass');
>
>
> if (!$con){die("can not connect: " . mysql_error());}
>
> mysql_select_db('richbull_top100',$con);
>
> $year=1960;
> $number=1;
>
> $result = mysql_query("SELECT atitle,artist,avid FROM A$year WHERE id =
> $number");
> if (!$result) { echo 'Could not run query: ' . mysql_error(); exit; }
> $vid = mysql_fetch_row($result);
>
>
> echo "{";
>
> echo 'file: "http://www.youtube.com/watch?v=$vid[2]"';
> echo ",";
> echo 'description: "$year # $number - $vid[1]"';
>
> echo "}";
>
>
>
>
>
> ?>
> ],
>
> "startparam":"start",
> "repeat":"list",
> "width":800,
> // adjust these 2 lines if you'd like to omit/resize playlist, etc:
> listbar:{position:'right',size:320},
> "height":450
>
> });
> </script>
>
> </body>
> </html>
>
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.
jstucklex(at)attglobal(dot)net
==================
|
|
|
|
Re: Problem with quotes in javascript output [message #181549 is a reply to message #181527] |
Tue, 21 May 2013 10:47 |
Denis McMahon
Messages: 634 Registered: September 2010
Karma: 0
|
Senior Member |
|
|
On Mon, 20 May 2013 12:17:26 -0400, richard wrote:
> This is not a javascript question ok?
Actually it is, because the problem seems to be that you're producing
invalid javascipt.
> The problem mainly deals with required quotes in the javascript output.
> Either single or double.
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 : "http://www.youtube.com/watch?v=$vid[2]",
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
javascript.
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: 'http://www.youtube.com/watch?v={$vid[2]}'";
echo ",";
echo "description: '$year # $number - {$vid[1]}'";
or
echo "file: \"http://www.youtube.com/watch?v={$vid[2]}\"";
echo ",";
echo "description: \"$year # $number - {$vid[1]}\"";
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, denismfmcmahon(at)gmail(dot)com
|
|
|
Re: Problem with quotes in javascript output [message #181551 is a reply to message #181549] |
Tue, 21 May 2013 11:59 |
Thomas 'PointedEars'
Messages: 701 Registered: October 2010
Karma: 0
|
Senior Member |
|
|
Denis McMahon wrote:
> 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 : "http://www.youtube.com/watch?v=$vid[2]",
> 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
> javascript.
ACK. “{ { … } }” is not a valid Object initializer; the property name (like
“{foo: { … } }”) is missing.
> 2) Some attributes
_property names_
> are quoted and others are not. Although javascript is
… non-existent, see <http://PointedEars.de/es-matrix>, but ECMAScript
implementations are (per Specification) …
> 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.
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.
> 3) Your php variable substitution isn't going to work for two reasons.
> You need something more like:
>
> echo "file: 'http://www.youtube.com/watch?v={$vid[2]}'";
> echo ",";
> echo "description: '$year # $number - {$vid[1]}'";
>
> or
>
> echo "file: \"http://www.youtube.com/watch?v={$vid[2]}\"";
> echo ",";
> echo "description: \"$year # $number - {$vid[1]}\"";
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: "http://www.youtube.com/watch?v=<?= addslashes($vid[2]) ?>",
description: "<?= addslashes("$year # $number - {$vid[1]}") ?>"
<?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': "http://www.youtube.com/watch?v={$vid[2]}",
'description': "$year # $number - {$vid[1]}"
)) ?>);
<?php … ?>
(The leading and trailing PHP sections are optional.)
PointedEars
--
realism: HTML 4.01 Strict
evangelism: XHTML 1.0 Strict
madness: XHTML 1.1 as application/xhtml+xml
-- Bjoern Hoehrmann
|
|
|