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?

1
  • By "skip", do you mean "escape," as in writing "...\"..."? Commented Feb 11, 2011 at 15:19

2 Answers 2

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);";
Sign up to request clarification or add additional context in comments.

13 Comments

@Umair, also, it gets even more fun if you have some html like <img src="..." alt="He said, \"Hi!\""/> ^_^
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, ', ", 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)
Ok got it. Do I need to escape others too? Like \r, \t, \b and any other?
@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)
|
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.

3 Comments

You're forgetting that your string'll be interpreted twice here, once in Java and once in Javascript.
Strings are "interpreted" in Java? Or you mean variables interpolation?
maybe "parsed" is a better word than "interpreted"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.