Results 1 to 7 of 7

Thread: [RESOLVED] Nested quotes with html and javascript

  1. #1

    Thread Starter
    Fanatic Member aconybeare's Avatar
    Join Date
    Oct 2001
    Location
    UK
    Posts
    772

    Resolved [RESOLVED] Nested quotes with html and javascript

    Hi,

    I'm trying to work out how to use nested quotes.

    The first link works, but the second fails because of the hilighted apostrophe

    Code:
    <html>
    <head>
    	<title>test</title>
    </head>
    <body>
    <div id="grid"></div>
    <script type="text/javascript">
    	var tableRow = New Array(1);
    
    	tableRow[0] = '<p><a href="#" onclick="alert(\'hello\');return false;">test 1</a></p>';
    	tableRow[1] = '<p><a href="#" onclick="alert(\'There\'s no place like home\');return false;">test 2</a></p>';
    
    	var txt = '';
    	for(var iLoop = 0; iLoop <= 1; iLoop++){
    		txt +=tableRow;
    	}
    	document.getElementById("grid").innerHTML = txt;
     </script>
    </body>
    </html>
    Any ideas will be greatly appreciated

    Cheers Al

  2. #2
    Frenzied Member tr333's Avatar
    Join Date
    Nov 2004
    Location
    /dev/st0
    Posts
    1,605

    Re: Nested quotes with html and javascript

    You need to insert an escaped slash character before the original escaped single quote character. The first two single quotes in the alert() method are printed as single quotes in the HTML, so adding a third quote character will mess this up. The extra slash is required to escape the quote when it is all printed into HTML. The javascript code at the bottom gives an alert box containing the HTML that would be printed by the first JS code so you can see where the extra escaped slash is required.

    I also removed the for loop as it was unnecessary. the join() method will combine all array elements together with a newline character separating them.

    HTML Code:
    1. <?xml version="1.0" encoding="utf-8"?>
    2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    3.         "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    4. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    5.  
    6. <head>
    7.     <title>test</title>
    8.     <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    9.     <meta http-equiv="Content-Style-Type" content="text/css" />
    10. </head>
    11.  
    12. <body>
    13. <div id="grid"></div>
    14. <script type="text/javascript">
    15.     var tableRow = new Array();
    16.  
    17.     tableRow[0] = '<p><a href="#" onclick="alert(\'hello\');">test 0</a></p>';
    18.     tableRow[1] = '<p><a href="#" onclick="alert(\'There\\\'s no place like home\');">test 1</a></p>';
    19.     tableRow[2] = '<p><a href="#" onclick="alert(\'hello2\');">test 2</a></p>';
    20.  
    21.     document.getElementById("grid").innerHTML = tableRow.join("\n");
    22. </script>
    23.  
    24.  
    25. <script type="text/javascript">
    26.     alert(document.getElementById("grid").innerHTML);
    27. </script>
    28. </body>
    29. </html>
    CSS layout comes in to the 21st century with flexbox!
    Just another Perl hacker,

  3. #3

    Thread Starter
    Fanatic Member aconybeare's Avatar
    Join Date
    Oct 2001
    Location
    UK
    Posts
    772

    Re: Nested quotes with html and javascript

    tr333,

    Thanks very much for your reply, much appreciated.

    I'd already tried what you've suggested and it didn't work, so must have been doing something else wrong, anyway if tried it again and it's working, so I'm happy.

    Cheers Al

  4. #4
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: [RESOLVED] Nested quotes with html and javascript

    You really should use the createElement and appendChild methods rather than innerHTML: The latter won't work in an XML document, which you should technically be sending XHTML 1.0 Strict as.

  5. #5

    Thread Starter
    Fanatic Member aconybeare's Avatar
    Join Date
    Oct 2001
    Location
    UK
    Posts
    772

    Re: [RESOLVED] Nested quotes with html and javascript

    Penagate,

    Thanks for the tip, I'll look into it

    Cheers Al

  6. #6
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Re: [RESOLVED] Nested quotes with html and javascript

    penagate: though practically in the wild, XHTML1 has lost itself to not being a real XML document thanks to the browser support. And they can't change that anymore, if they make a dramatical change in one browser, that'll break tons of sites that use XHTML in the incorrect way. There really is no way to ensure the backwards compatibility of the current sites.

    So I believe we won't ever see a browser that handles XHTML1 exactly as it should be. And that is why we keep seeing innerHTML working practise.

    (Firefox 1 tried to be different, they ended up restoring the functionality. You just don't want to break thousands of sites only to force usage of a standard better: you only get negative feedback and angry users.)

  7. #7
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: [RESOLVED] Nested quotes with html and javascript

    Yes, I realise, but createElement/appendChild are superior anyway.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width