Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Reply

Join Date: Jan 2008
Posts: 6
Reputation: jianwu_chen is an unknown quantity at this point 
Solved Threads: 0
jianwu_chen jianwu_chen is offline Offline
Newbie Poster

CDATA in HTML

 
0
  #1
Jan 8th, 2008
I'm trying to put some text which will confuse HTML parser inside the CDATA section for javascript to access. But seems it won't work in both firefox and Internet Explorer. In Firefix, it will treat it as comment, it's still possible to remove the <!-- and --> go get back the original content, in IE it's worse, CDATA seems be totally ignored. No way to get the content back.

  1.  
  2. <html>
  3. <body>
  4. <div id="test"><![CDATA[Testing Data here]]></div>
  5. </body>
  6. </html>
  7. <script>
  8. alert(document.getElementById("test").innerHTML );
  9. alert(document.getElementById("test").firstChild.data);
  10. </script>

Anybody has some idea to get back the CDATA in the HTML using javascript?

Thanks
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 3,193
Reputation: MidiMagic has a spectacular aura about MidiMagic has a spectacular aura about 
Solved Threads: 162
MidiMagic's Avatar
MidiMagic MidiMagic is offline Offline
Posting Sensei

Re: CDATA in HTML

 
0
  #2
Jan 8th, 2008
Don't embed scripts. Call functions in external scripts instead.

Your quotes need to be escaped.
Daylight-saving time uses more gasoline
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 6
Reputation: jianwu_chen is an unknown quantity at this point 
Solved Threads: 0
jianwu_chen jianwu_chen is offline Offline
Newbie Poster

Re: CDATA in HTML

 
0
  #3
Jan 8th, 2008
thanks for the reply. But maybe you mis-understand my issue.

Here my problem is not the javascript code itself. But the java script can't assess the data inside
<![[CDATA .... ]]
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 1,050
Reputation: digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice 
Solved Threads: 65
Moderator
digital-ether's Avatar
digital-ether digital-ether is offline Offline
Veteran Poster

Re: CDATA in HTML

 
0
  #4
Jan 9th, 2008
Originally Posted by jianwu_chen View Post
thanks for the reply. But maybe you mis-understand my issue.

Here my problem is not the javascript code itself. But the java script can't assess the data inside
<![[CDATA .... ]]
Test it with XHTML and see if you get anything.
www.fijiwebdesign.com - web design and development and fun
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 3,193
Reputation: MidiMagic has a spectacular aura about MidiMagic has a spectacular aura about 
Solved Threads: 162
MidiMagic's Avatar
MidiMagic MidiMagic is offline Offline
Posting Sensei

Re: CDATA in HTML

 
0
  #5
Jan 12th, 2008
If you need data constants, put them in the script, not in the HTML.
Daylight-saving time uses more gasoline
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 6
Reputation: jianwu_chen is an unknown quantity at this point 
Solved Threads: 0
jianwu_chen jianwu_chen is offline Offline
Newbie Poster

Re: CDATA in HTML

 
0
  #6
Jan 12th, 2008
Originally Posted by MidiMagic View Post
If you need data constants, put them in the script, not in the HTML.
The reason i want to put it in CDATA, is that I want to put some template html data which may include many lines of HTML source. While it's possible to put the whole template inside the javascript string, but it's quite ugly as i have to use the code like: "line1\n" + "line2" + ....., and if i have single or double quote inside the template, i have escape that, which will make the javascript code more ugly. I just want a cleaner way to let javascript read the template from some external source.
Last edited by jianwu_chen; Jan 12th, 2008 at 4:13 am.
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 1,050
Reputation: digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice 
Solved Threads: 65
Moderator
digital-ether's Avatar
digital-ether digital-ether is offline Offline
Veteran Poster

Re: CDATA in HTML

 
0
  #7
Jan 12th, 2008
Originally Posted by jianwu_chen View Post
The reason i want to put it in CDATA, is that I want to put some template html data which may include many lines of HTML source. While it's possible to put the whole template inside the javascript string, but it's quite ugly as i have to use the code like: "line1\n" + "line2" + ....., and if i have single or double quote inside the template, i have escape that, which will make the javascript code more ugly. I just want a cleaner way to let javascript read the template from some external source.
You can place the data in a separate file and load it into the page later using XMLHttpRequest or similar, if you don't need it right away.

You can place it in the HTML of the page and apply a CSS style to hide it.

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <div style="display:none;">data here...</div>
Last edited by digital-ether; Jan 12th, 2008 at 9:38 am.
www.fijiwebdesign.com - web design and development and fun
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 6
Reputation: jianwu_chen is an unknown quantity at this point 
Solved Threads: 0
jianwu_chen jianwu_chen is offline Offline
Newbie Poster

Re: CDATA in HTML

 
0
  #8
Jan 13th, 2008
Originally Posted by digital-ether View Post
You can place the data in a separate file and load it into the page later using XMLHttpRequest or similar, if you don't need it right away.

You can place it in the HTML of the page and apply a CSS style to hide it.

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <div style="display:none;">data here...</div>
You are right. I do use XMLHttpRequest, in case that the template is only required during runtime depends on users interaction. But for some templates which are always necessary to be loaded together with the master HTML. The AJAX way is not efficient and also increases the code complexity with no benifits. That's why I want a easier way.

I do use <div style="display:none;"> tag for that. It's ok for most of the HTML templates. But somehow, if there are special characters inside the template such as "<", "#" etc, the HTML parser will build up a wrong DOM which causes problems, i also don't want put a lot of XML entities such as &lt; which is really ugly. that's why I want to use CDATA tag inside the above <DIV> tag to prevent HTML parser manipulate the content of the template. But seems this is not a solution as HTML parser won't interprete CDATA correctly for most of browsers, even I use XHTML.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 1
Reputation: owenoak is an unknown quantity at this point 
Solved Threads: 0
owenoak owenoak is offline Offline
Newbie Poster

Re: CDATA in HTML

 
0
  #9
Jan 16th, 2009
One way to do it is to wrap the HTML for your template in a <script> tag with a non-javascript "type", eg:

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <script type='html/template' id='myTemplate'>
  2. ... your html code here...
  3. </script>

I've just tested this in IE7 (I believe it works in IE6), FF3 and Webkit 3.2) and it works great. You can get the HTML for each template by id:

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. var myHTML = document.getElementById('myTemplate').innerHTML;

Note that you can use any "type" you want there, as long as it is not something that the browser will try to interpret (like text/javascript or visual basic or something). I'm not sure if this will pass an (x)html tidy application or not.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 6
Reputation: jianwu_chen is an unknown quantity at this point 
Solved Threads: 0
jianwu_chen jianwu_chen is offline Offline
Newbie Poster

Re: CDATA in HTML

 
0
  #10
Sep 1st, 2009
One solution for that is to put the data between <textarea> and make it hidden.
Reply With Quote Quick reply to this message  
Reply

Message:



Similar Threads
Other Threads in the JavaScript / DHTML / AJAX Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC