Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #9499

Re: Problem with quotes in javascript output

Message-ID <1592783.kto8HJv2Jy@PointedEars.de> (permalink)
From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Organization PointedEars Software (PES)
Date 2013-05-21 13:59 +0200
Subject Re: Problem with quotes in javascript output
Newsgroups comp.lang.php
References <54lw0fd343qv$.gm5ydjjd60j9$.dlg@40tude.net> <knfjcj$e55$1@dont-email.me>

Show all headers | View raw


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

Back to comp.lang.php | Previous | Next — Previous in thread | Find similar | Unroll thread


Thread

Problem with quotes in javascript output richard <noreply@example.com> - 2013-05-20 12:17 -0400
  Re: Problem with quotes in javascript output Jerry Stuckle <jstucklex@attglobal.net> - 2013-05-20 12:53 -0400
  Re: Problem with quotes in javascript output richard <noreply@example.com> - 2013-05-20 14:12 -0400
  Re: Problem with quotes in javascript output Denis McMahon <denismfmcmahon@gmail.com> - 2013-05-21 10:47 +0000
    Re: Problem with quotes in javascript output Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-05-21 13:59 +0200

csiph-web