File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes HTML, CSS and JavaScript and the fly likes How do I nest Strings within Strings? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "How do I nest Strings within Strings?" Watch "How do I nest Strings within Strings?" New topic
Author

How do I nest Strings within Strings?

Brian Legg
Ranch Hand

Joined: Nov 07, 2008
Posts: 488
Here's a String:

document.write("Hello!")

Here's a String in a String:

onClick='myMethod("SomeText")'

How do I go further to have a String in a String in a String.... and beyond? I just started in JavaScript a few days ago and I am already finding myself dodging this issue so I know I will hit it later.

Thanks in advance!


SCJA
~Currently preparing for SCJP6
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
Not sure what you mean.

Eric
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61436
    
  67

Escape the quote characters with the \.

E.g. 'My name is O\'Brien'


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Brian Legg
Ranch Hand

Joined: Nov 07, 2008
Posts: 488
Thanks Bear. I don't think you get what I mean though. Take the following command...

onClick='addLink("<a href='www.mylink.com'>Hello!</a>")'

This is just an example, but as you can see my onClick takes a String. Inside of that there is another String which is some html code. Inside of that String there is another String for an Href link. As soon as it reads the ' before the www the onClick is going to think it's at the end of the statement when it's not. I'm wanting to know how to handle these types of situations where you have more than 2 Strings nested inside each other.

I realize that the previous example would use \"www.mylink.com\" to escape the quotes... so it's a bad example, but I hope you get my question.

Thanks!

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61436
    
  67

Answer remains the same.
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
Bear Bibeault wrote:Answer remains the same.


Actually is does not in this case. You can not escape the single quote with a \ since we are dealing with an HTML attribute and not a plain old JavaScript string.

In that case you need to use an entities



OR

you need to change it up and use the escaping Bear talked about



Eric
Brian Legg
Ranch Hand

Joined: Nov 07, 2008
Posts: 488
Interesting. Thanks for the help guys!
 
 
subject: How do I nest Strings within Strings?