1

I have a long very long html which needs to be enclosed in Javascript string which in turn is enclosed in Java string as follows:

String html = "javascript:var html='...all goes here...';void(0);";

Now where is written ...all goes here... , there is all html including " and ' and even other special characters. Can I skip them in the Java way?

Umair A.
  • 6,690
  • 20
  • 83
  • 130

2 Answers2

2

In most languages double quotes can be placed inside double-quoted string by escaping them:

"This is a quoted string: \"I'm a quoted string\"."

The need of such thing (inserting js code with strings into Java string) may indicate, that your code design isn't ok.

kirilloid
  • 14,011
  • 6
  • 38
  • 52
2

Here you get to the fun of strings interpreted multiple times. your " quotes need to be escaped for java, but your ' quotes need to be escaped for javascript. Thus, your " quotes you can escape normally, but your ' quotes need the \ character to be in front of them when the javascript is interpreted, so you need a literal \ in your java string (or \, an escaped ). thus, if you set your html variable to the html:

<span class="class">Here's Johnny!</span>

you'll need to do:

String html = "javascript:var html='<span class=\"class\">Here\\'s Johnny!</span>';void(0);";
jswolf19
  • 2,303
  • 15
  • 16
  • @Umair, also, it gets even more fun if you have some html like `He said, \` ^_^ – jswolf19 Feb 11 '11 at 15:52
  • ok your point worked. Can you tell me if there are other characters than ' and " which I need to replace? I myself found "\n" which needs to be replaced with "\\n". Why this is so? – Umair A. Feb 16 '11 at 10:01
  • @Umair, `'`, `"`, and `\\` should be the only characters you have to be careful about, `"` because Java interprets it as the end of the string, `\` because Java interprets it as the start of an escape sequence, and `'` because javascript interprets it as the end of the string in the javascript code. If you have `\n` in your string, then Java will change this to a newline character, which means that in your javascript you will have a newline in the middle of your string instead of `\n` telling javascript to output a newline. (`\\n` tells Java to put `\n` in the string) – jswolf19 Feb 16 '11 at 10:09
  • Ok got it. Do I need to escape others too? Like \r, \t, \b and any other? – Umair A. Feb 16 '11 at 10:16
  • @Umair, as I said before, you need to escape any `\ ` that you want to go to javascript. So if you want a tab character to appear in the javascript, then `\t` is fine, but if you want `\t` to appear in the javascript, then you need to escape the `\\ ` to `\\ ` (so you will have a `\\t`, in effect) – jswolf19 Feb 16 '11 at 10:19
  • Ok. I was using .replace("'", "\\'").replace('"', '\"'); before do I need to use .replace("'", "\\'").replace('"', '\"').replace("\", "\\"); now? – Umair A. Feb 16 '11 at 10:39
  • @Umair. your original code does not mention using the `replace()` method of the string class. If that's how you're excaping your string, then the `"` characters should already be escaped as necessary, unless they occur within a double-quoted string. `replace()` may not be good enough depending on the content of the html... At the very least, you need to put the replace for the backslash first, otherwise all your `\'` s will become `\\'` s, which is definitely not what you would want. – jswolf19 Feb 16 '11 at 10:54
  • Correct. Instead of replace what are my options? – Umair A. Feb 16 '11 at 12:04
  • @Umair, Well, with your example, you showed a hard-coded string, so you could edit the hard-coded string to add the necessary slashes as necessary. I can't really give you more information unless I see code that more closely models your actual code. – jswolf19 Feb 16 '11 at 12:24
  • actually the string is very long HTML as I previously mentioned, I just hard coded it here to save space. Assume the string is coming from some database table and it's pure HTML. Helpful? – Umair A. Feb 16 '11 at 12:41
  • @Umair, The way I'm interpreting what you are doing, \n shouldn't have caused you problems unless the issue occurred when the data was inserted into the database... – jswolf19 Feb 16 '11 at 13:23
  • There are many \n characters in description column of database table. This is causing problems and when I escaped it the Java way, it resolved the issue. – Umair A. Feb 16 '11 at 13:35
  • Do they show up as \n in the database or as a newline? If they show up as a newline, then you will have to call `replace("\n", "\\n")`, and you will have to create similar calls for other characters such as \r and \t if they might appear. However, I think it would be easier, and less prone to missed errors to use ajax techniques (`httpRequest`, etc.) to get the html text or to put the text in a hidden div tag for your javascript to access (if you trust the source of the html in the database). Then, you shouldn't have to worry about any escaping. – jswolf19 Feb 16 '11 at 13:52