HTML String Inside Nested String
Asked Answered
G

2

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?

Gridley answered 11/2, 2011 at 15:16 Comment(1)
By "skip", do you mean "escape," as in writing "...\"..."?Spillway
T
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);";
Timepiece answered 11/2, 2011 at 15:23 Comment(13)
@Umair, also, it gets even more fun if you have some html like <img src="..." alt="He said, \"Hi!\""/> ^_^Timepiece
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?Gridley
@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)Timepiece
Ok got it. Do I need to escape others too? Like \r, \t, \b and any other?Gridley
@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)Timepiece
Ok. I was using .replace("'", "\\'").replace('"', '\"'); before do I need to use .replace("'", "\\'").replace('"', '\"').replace("\", "\\"); now?Gridley
@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.Timepiece
Correct. Instead of replace what are my options?Gridley
@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.Timepiece
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?Gridley
@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...Timepiece
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.Gridley
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.Timepiece
D
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.

Dine answered 11/2, 2011 at 15:19 Comment(3)
You're forgetting that your string'll be interpreted twice here, once in Java and once in Javascript.Timepiece
Strings are "interpreted" in Java? Or you mean variables interpolation?Dine
maybe "parsed" is a better word than "interpreted"Timepiece
T
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);";
Timepiece answered 11/2, 2011 at 15:23 Comment(13)
@Umair, also, it gets even more fun if you have some html like <img src="..." alt="He said, \"Hi!\""/> ^_^Timepiece
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?Gridley
@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)Timepiece
Ok got it. Do I need to escape others too? Like \r, \t, \b and any other?Gridley
@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)Timepiece
Ok. I was using .replace("'", "\\'").replace('"', '\"'); before do I need to use .replace("'", "\\'").replace('"', '\"').replace("\", "\\"); now?Gridley
@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.Timepiece
Correct. Instead of replace what are my options?Gridley
@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.Timepiece
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?Gridley
@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...Timepiece
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.Gridley
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.Timepiece

© 2022 - 2024 — McMap. All rights reserved.