最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

java - HTML String Inside Nested String - Stack Overflow

programmeradmin3浏览0评论

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
Add a ment  | 

2 Answers 2

Reset to default 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);";

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)

  1. 暂无评论