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.

<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

Recommended Answers

All 11 Replies

Don't embed scripts. Call functions in external scripts instead.

Your quotes need to be escaped.

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 .... ]]

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.

If you need data constants, put them in the script, not in the HTML.

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.

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.

<div style="display:none;">data here...</div>

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.

<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.

One way to do it is to wrap the HTML for your template in a <script> tag with a non-javascript "type", eg:

<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:

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.

One solution for that is to put the data between <textarea> and make it hidden.

<![CDATA[
content  goes here
]]>

Just do what I do. Use PHP which is made for situations such as this.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.