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?
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?
Share Improve this question asked Feb 11, 2011 at 15:16 Umair A.Umair A. 6,87320 gold badges85 silver badges134 bronze badges 1-
By "skip", do you mean "escape," as in writing
"...\"..."
? – Matt Ball Commented Feb 11, 2011 at 15:19
2 Answers
Reset to default 2Here 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);";
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.