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


Groups > comp.lang.javascript > #28795

Re: How to pass a php variable to a javascript file (function)?

From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Newsgroups comp.lang.javascript
Subject Re: How to pass a php variable to a javascript file (function)?
Date 2015-10-02 21:37 +0200
Organization PointedEars Software (PES)
Message-ID <1739227.XXYPReGQTy@PointedEars.de> (permalink)
References <r6jPx.32747$nl4.14152@fx02.am4> <XnsA5276328DD4DEeejj99@194.109.6.166> <mulldu$lli$1@news.albasani.net>

Show all headers | View raw


Stefan Weiss wrote:

> On 2015-10-02 09:44, Evertjan. wrote:
>>>              var some_string = <?php echo json_encode($sometext); ?>;
>> 
>> This is totally errorprone, you need to enclose the litteral string in
>> '-s or "-s, ensuring that the ones used do not appear in the inner text.
> 
> No, json_encode() already takes care of quoting and character escaping;
> adding extra quotes around the result would be a syntax error.

ACK.
 
> That doesn't mean that json_encode() by itself is enough to safely
> escape strings as JavaScript literals. What if $sometext contains
> "</script>"?

Not a problem in HTML5 or XHTML(5), but in HTML < 5.
 
> What actually has to be escaped - in addition to characters like quotes,
> newlines, etc - depends on which HTML version is used.
> For HTML5, the process and some pitfalls are described here:
> http://www.w3.org/TR/html5/scripting-1.html#restrictions-for-contents-of-script-elements
> 
> Short version:
> 
> | Note: The easiest and safest way to avoid the rather strange
> | restrictions described in this section is to always escape "<!--" as
> | "<\!--", "<script" as "<\script", and "</script" as "<\/script"  […]

And it is wrong *again* (see below).  For example, if you had a RegExp 
literal in your ECMAScript or Perl code to match “<script” (which as for 
ECMAScript code can be useful for retrieving embedded data), which would 
likely be /<script…/, then changing that to /<\script…/ would attempt to 
match “<”, followed by ECMAScript/Perl whitespace, followed by “cript…” 
instead.

For another example, as can be seen further below that note, the HTML5 
Specification (fortunately) does not even define syntax that would require 
“</script>” in a “script” element to be escaped in any way:

| script        = outer *( comment-open inner comment-close outer )
| 
| outer         = < any string that doesn't contain a substring that matches 
| not-in-outer >
| not-in-outer  = comment-open
| inner         = < any string that doesn't contain a substring that matches 
| not-in-inner >
| not-in-inner  = comment-close / script-open

(No “script-close” there.)

Presumably, this part of the note incorporates escape recommendations for 
HTML < 5, where the CDATA content of a SCRIPT element would end when the SGL 
parser encountered “</” (ETAGO) as it claims that “for historical reasons, 
parsing of script blocks in HTML is a strange and exotic practice that acts 
unintuitively in the face of these sequences.”

Which *actually* means: The authors of this paragraph did not know what the 
hell they were doing back then [when they wrote or read the HTML 4.01 
Specification], they still do not know, and they are projecting that on 
their unfortunate readership.  The record shows that one cannot trust the 
authors of HTML Specifications regarding advice relevant to Web programming 
languages, in particular ECMAScript implementations.  (Fortunately, as 
always, Notes are not normative.)

In fact, the easiest and safest way to avoid the issue is to put the script 
code in a separate file, and use the “src” attribute of the “script” element 
to include the corresponding resource.  Yes, that means another request, but 
you can’t have everything.

>>> So in doSomething I want to be able to use this some_string created in
>>> php file. How can I do it?
>> So you cannot.
>> 
>> Why don't you create that string on the "index.html" PAGE,
>> that you probably will have to rename to "index.php".
> 
> Thats one way to do it. Unfortunately, we don't know enough about OP's
> project to give any meaningful advice. For example, if we knew that
> `some_string` is relatively short, we could suggest passing it along to
> index.html in the query string.
> 
>   location.href = "index.html?s=" + encodeURIComponent(some_string);
> 
> The script included from index.html could then read it back from the
> query string.

It is important to emphasize “relatively short”, because this approach is 
subject to IE URL length limits which can be easily exceeded if the argument 
to encodeURIComponent() contains Unicode characters with code points beyond 
the US-ASCII range (as URI percent-encoding is UTF-8 based).
 
> In general, mixing dynamic and static files (PHP and plain HTML) like
> this tends to get messy.

It depends on the application.  There is no good reason that PHP should 
parse files in which there are no proper PHP sections, and every reason to 
avoid that.  But doing things client-side that can be done server-side is 
often not the correct approach.

-- 
PointedEars (Zend Certified PHP Engineer)
FAQ: <http://PointedEars.de/faq> | SVN: <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-matrix>
Please do not cc me. / Bitte keine Kopien per E-Mail.

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


Thread

How to pass a php variable to a javascript file (function)? JiiPee <no@notvalid.com> - 2015-10-02 00:13 +0100
  Re: How to pass a php variable to a javascript file (function)? "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-10-02 09:44 +0200
    Re: How to pass a php variable to a javascript file (function)? Stefan Weiss <krewecherl@gmail.com> - 2015-10-02 12:14 +0200
      Re: How to pass a php variable to a javascript file (function)? JiiPee <no@notvalid.com> - 2015-10-02 11:43 +0100
      Re: How to pass a php variable to a javascript file (function)? JiiPee <no@notvalid.com> - 2015-10-02 11:58 +0100
        Re: How to pass a php variable to a javascript file (function)? Stefan Weiss <krewecherl@gmail.com> - 2015-10-02 13:38 +0200
          Re: How to pass a php variable to a javascript file (function)? JiiPee <no@notvalid.com> - 2015-10-02 12:53 +0100
            Re: How to pass a php variable to a javascript file (function)? JiiPee <no@notvalid.com> - 2015-10-02 13:46 +0100
            Re: How to pass a php variable to a javascript file (function)? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-10-02 13:51 +0100
          Re: How to pass a php variable to a javascript file (function)? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-10-02 21:42 +0200
            Re: How to pass a php variable to a javascript file (function)? Stefan Weiss <krewecherl@gmail.com> - 2015-10-03 00:33 +0200
              Re: How to pass a php variable to a javascript file (function)? JiiPee <no@notvalid.com> - 2015-10-02 23:45 +0100
              Re: How to pass a php variable to a javascript file (function)? Tim Streater <timstreater@greenbee.net> - 2015-10-02 23:55 +0100
            Re: How to pass a php variable to a javascript file (function)? JiiPee <no@notvalid.com> - 2015-10-03 00:02 +0100
          Re: How to pass a php variable to a javascript file (function)? JiiPee <no@notvalid.com> - 2015-10-02 23:49 +0100
      Re: How to pass a php variable to a javascript file (function)? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-10-02 21:37 +0200
        Re: How to pass a php variable to a javascript file (function)? Stefan Weiss <krewecherl@gmail.com> - 2015-10-03 00:34 +0200
          Re: How to pass a php variable to a javascript file (function)? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-10-03 04:35 +0200
            Re: How to pass a php variable to a javascript file (function)? Stefan Weiss <krewecherl@gmail.com> - 2015-10-05 01:11 +0200
              Re: How to pass a php variable to a javascript file (function)? Tim Streater <timstreater@greenbee.net> - 2015-10-05 09:13 +0100
              Re: How to pass a php variable to a javascript file (function)? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-10-06 03:30 +0200
                Re: How to pass a php variable to a javascript file (function)? Stefan Weiss <krewecherl@gmail.com> - 2015-10-10 00:27 +0200
        Re: How to pass a php variable to a javascript file (function)? Joao Rodrigues <groups_jr-1@yahoo.com.br> - 2015-10-03 15:15 -0300
          Re: How to pass a php variable to a javascript file (function)? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-10-03 20:34 +0200
            Re: How to pass a php variable to a javascript file (function)? Joao Rodrigues <groups_jr-1@yahoo.com.br> - 2015-10-03 15:59 -0300
              Re: How to pass a php variable to a javascript file (function)? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-10-03 22:15 +0200
    Re: How to pass a php variable to a javascript file (function)? JiiPee <no@notvalid.com> - 2015-10-02 11:36 +0100
      Re: How to pass a php variable to a javascript file (function)? "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-10-02 16:34 +0200
        Re: How to pass a php variable to a javascript file (function)? JiiPee <no@notvalid.com> - 2015-10-02 16:00 +0100
          Re: How to pass a php variable to a javascript file (function)? Tim Streater <timstreater@greenbee.net> - 2015-10-02 16:03 +0100
            Re: How to pass a php variable to a javascript file (function)? JiiPee <no@notvalid.com> - 2015-10-02 17:08 +0100
  solution, thanks, seems like one suggestion lead to a solution JiiPee <no@notvalid.com> - 2015-10-02 17:07 +0100
    Re: solution, thanks, seems like one suggestion lead to a solution "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-10-02 20:29 +0200

csiph-web