开发者

HTML String Inside Nested String

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 al开发者_Go百科l html including " and ' and even other special characters. Can I skip them in the Java way?


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);";


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜