FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » Problem with quotes in javascript output
Show: Today's Messages :: Polls :: Message Navigator
Return to the default flat view Create a new topic Submit Reply
Re: Problem with quotes in javascript output [message #181551 is a reply to message #181549] Tue, 21 May 2013 11:59 Go to previous message
Thomas 'PointedEars'  is currently offline  Thomas 'PointedEars'
Messages: 701
Registered: October 2010
Karma:
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
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: comma placement problem
Next Topic: no date header in mail()
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Sun Nov 24 12:10:52 GMT 2024

Total time taken to generate the page: 0.04534 seconds