| | |
CDATA in HTML
Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Jan 2008
Posts: 6
Reputation:
Solved Threads: 0
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.
Anybody has some idea to get back the CDATA in the HTML using javascript?
Thanks
html Syntax (Toggle Plain Text)
<html> <body> <div id="test"><![CDATA[Testing Data here]]></div> </body> </html> <script> alert(document.getElementById("test").innerHTML ); alert(document.getElementById("test").firstChild.data); </script>
Anybody has some idea to get back the CDATA in the HTML using javascript?
Thanks
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!
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
•
•
Join Date: Jan 2008
Posts: 6
Reputation:
Solved Threads: 0
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.
•
•
•
•
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 it in the HTML of the page and apply a CSS style to hide it.
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
<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!
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
•
•
Join Date: Jan 2008
Posts: 6
Reputation:
Solved Threads: 0
•
•
•
•
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)
<div style="display:none;">data here...</div>
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 < 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.
•
•
Join Date: Jan 2009
Posts: 1
Reputation:
Solved Threads: 0
One way to do it is to wrap the HTML for your template in a <script> tag with a non-javascript "type", eg:
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:
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.
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
<script type='html/template' id='myTemplate'> ... your html code here... </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)
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.
![]() |
Similar Threads
- vbscript variable within a CDATA node (XML, XSLT and XPATH)
- Display a movie clip on a button click (Graphics and Multimedia)
- HTML in CDDATA wont parse (RSS, Web Services and SOAP)
- XML and hyperlink as one of the data (RSS, Web Services and SOAP)
- replace address - how to? (PHP)
- vBulletin mod_rewrite for vB 3.0.7 (Existing Scripts)
Other Threads in the JavaScript / DHTML / AJAX Forum
- Previous Thread: Javascript Not Displaying Php Styleing
- Next Thread: Javascript to create dynamic drop down list
| Thread Tools | Search this Thread |
acid2 ajax ajaxcode ajaxexample ajaxhelp ajaxjspservlets animate automatically beta box browser bug captchaformproblem checkbox close codes createrange() css cursor debugger decimal dependent disablefirebug dom download dropdown editor element engine enter error events explorer ext file firefox form forms frameworks getselection google gwt gxt hiddenvalue highlightedword hint html htmlform ie7 ie8 iframe index internet java javascript javascripthelp2020 jawascriptruntimeerror jquery jsf jsp jump listbox maps masterpage math media menu microsoft mp4 object onmouseoutdivproblem onreadystatechange paypal pdf php player position programming progressbar prototype redirect regex runtime safari scale scriptlets search security select size software sql text textarea unicode w3c window windowofwords windowsxp \n






