Groups | Search | Server Info | Keyboard shortcuts | Login | Register
Groups > comp.lang.java.programmer > #4855
| From | Travers Naran <tnaran@gmail.com> |
|---|---|
| Newsgroups | comp.lang.java.programmer |
| Subject | Re: Java code to output escaped Javascript? |
| Date | 2011-06-01 07:47 -0700 |
| Organization | A noiseless patient Spider |
| Message-ID | <is5je0$jt8$1@dont-email.me> (permalink) |
| References | <b9498e86-bb2c-4838-9ad1-61a71f3980e0@e35g2000yqc.googlegroups.com> |
> 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.
>
> /* 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;
> }
That's how I would do it, but if you want existing code, maybe you can
adapt something from this library:
http://www.json.org/java/
Specifically, this class:
http://www.json.org/javadoc/org/json/JSONWriter.html
Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar
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