Path: csiph.com!news.mixmin.net!weretis.net!feeder1.news.weretis.net!news.solani.org!.POSTED!not-for-mail From: Thomas 'PointedEars' Lahn Newsgroups: comp.lang.javascript Subject: Re: How to pass a php variable to a javascript file (function)? Date: Fri, 02 Oct 2015 21:37:50 +0200 Organization: PointedEars Software (PES) Lines: 106 Message-ID: <1739227.XXYPReGQTy@PointedEars.de> References: Reply-To: Thomas 'PointedEars' Lahn Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8Bit X-Trace: solani.org 1443814672 31066 eJwNwoERACEIA7CVACnqOHyv7D+CfwlWeXFnoRLzoyDC5jKljoZrceGiT4S186ht/HB/HqYHLpgRfA== (2 Oct 2015 19:37:52 GMT) X-Complaints-To: abuse@news.solani.org NNTP-Posting-Date: Fri, 2 Oct 2015 19:37:52 +0000 (UTC) User-Agent: KNode/4.14.2 X-User-ID: eJwFwQERADEIAzBLz61AkTMG9S/hEz9h8RLhAZeLQk61Nm5DS26d/mBPhPytvqPutUlN3gVtqA45abVWP4v5FuA= Cancel-Lock: sha1:13P2yibewUPzv/3pn/m6SqQwwsU= X-NNTP-Posting-Host: eJwFwQERADEIAzBLHVBG5ezg8S/hE3qe7BvJDC5XwbJ4ektgoKkLbenAQGRL4/X5HOvnjP0B/bQQHA== Xref: csiph.com comp.lang.javascript:28795 Stefan Weiss wrote: > On 2015-10-02 09:44, Evertjan. wrote: >>> var some_string = ; >> >> 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 > ""? 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 "