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


Groups > comp.lang.java.programmer > #4861

Re: Java code to output escaped Javascript?

Path csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!border3.nntp.dca.giganews.com!border1.nntp.dca.giganews.com!nntp.giganews.com!postnews.google.com!h9g2000yqk.googlegroups.com!not-for-mail
From "laredotornado@zipmail.com" <laredotornado@gmail.com>
Newsgroups comp.lang.java.programmer
Subject Re: Java code to output escaped Javascript?
Date Wed, 1 Jun 2011 09:58:41 -0700 (PDT)
Organization http://groups.google.com
Lines 39
Message-ID <0dd6b678-e96e-4bd0-b133-9d28e25308fe@h9g2000yqk.googlegroups.com> (permalink)
References <b9498e86-bb2c-4838-9ad1-61a71f3980e0@e35g2000yqc.googlegroups.com> <is5jk2$l36$1@dont-email.me>
NNTP-Posting-Host 12.35.97.2
Mime-Version 1.0
Content-Type text/plain; charset=ISO-8859-1
Content-Transfer-Encoding quoted-printable
X-Trace posting.google.com 1306947521 14184 127.0.0.1 (1 Jun 2011 16:58:41 GMT)
X-Complaints-To groups-abuse@google.com
NNTP-Posting-Date Wed, 1 Jun 2011 16:58:41 +0000 (UTC)
Complaints-To groups-abuse@google.com
Injection-Info h9g2000yqk.googlegroups.com; posting-host=12.35.97.2; posting-account=siV97QoAAACYFQde4xADFFFlMTK0JZ44
User-Agent G2/1.0
X-Google-Web-Client true
X-Google-Header-Order HUALESNKRC
X-HTTP-UserAgent Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; en-US; rv:1.9.2.2) Gecko/20100316 Firefox/3.6.2,gzip(gfe)
Xref x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:4861

Show key headers only | View raw


On Jun 1, 9:50 am, Joshua Cranmer <Pidgeo...@verizon.invalid> wrote:
> On 06/01/2011 09:11 AM, laredotornado wrote:
>
> > Hi,
>
> > I'm using Java 6.  I want to output code for a Javascript variable ...
>
> >                    String jsValue = escapeForJS(value);
> >                    String expression = "storedVars['myVar'] = \"" + jsValue + "\";";
>
> > Is there anything standard that will do this?  I came up with my own
> > function, but I don't want to worry about leaving anything out.
>
> Any Java->JSON library worth its salt should be able to do this.
>
> >    /* creates a JS expression that can be used within quotes. */
> >    private String escapeForJS(String value) {
> >            value = value.replace("\n", "\\n");
> >            value = value.replace("\r", "\\r");
> >            value = value.replace("\"", "\\\"");
> >            return value;
> >    }
>
> You also forgot `\' as well as every character in the range
> '\u0000'-'\u001f' and '\u007f-\uffff' [if you have to worry about
> non-BMP characters, keep in mind that JS is like Java in that it has the
> same UCS-2/UTF-16 hairyness].
>
> --
> Beware of bugs in the above code; I have only proved it correct, not
> tried it. -- Donald E. Knuth

Yeah, you hit on exactly what I was talking -- a bunch of characters
that it is not practical to hard-code for.  I'm not outputting JSON
objects, so is the Java -> JSON route you suggest still the way to go?
- Dave

Back to comp.lang.java.programmer | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

Java code to output escaped Javascript? laredotornado <laredotornado@zipmail.com> - 2011-06-01 06:11 -0700
  Re: Java code to output escaped Javascript? Travers Naran <tnaran@gmail.com> - 2011-06-01 07:47 -0700
  Re: Java code to output escaped Javascript? Joshua Cranmer <Pidgeot18@verizon.invalid> - 2011-06-01 10:50 -0400
    Re: Java code to output escaped Javascript? "laredotornado@zipmail.com" <laredotornado@gmail.com> - 2011-06-01 09:58 -0700
    Re: Java code to output escaped Javascript? Lawrence D'Oliveiro <ldo@geek-central.gen.new_zealand> - 2011-06-02 10:17 +1200
      Re: Java code to output escaped Javascript? Joshua Cranmer <Pidgeot18@verizon.invalid> - 2011-06-01 21:47 -0400

csiph-web