Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes HTML, CSS and JavaScript and the fly likes Adding CDATA tag in script tag Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Adding CDATA tag in script tag" Watch "Adding CDATA tag in script tag" New topic
Author

Adding CDATA tag in script tag

Jagdeep Sharma
Ranch Hand

Joined: May 24, 2010
Posts: 121

Guys, I am using below code to prevent script tag validation parsing.
I am using is correctly.



Is there any mistake please let me know
Jagdeep Sharma
Ranch Hand

Joined: May 24, 2010
Posts: 121

what happen guys? Nobody wants to answer my question. Please reply.
Christian Johansen
author
Ranch Hand

Joined: Oct 03, 2010
Posts: 46
If you're using XHTML, then strictly I guess the CDATA blocks are required. But really, none of this is needed in practice. The comment will probably trick the validator, but unless you're targeting Netscape 2.0 I don't think you actually need them. In any case, a better approach would be to use external scripts, which will clean up your page, and help with front-end performance.
Jagdeep Sharma
Ranch Hand

Joined: May 24, 2010
Posts: 121

Christian Johansen wrote:If you're using XHTML, then strictly I guess the CDATA blocks are required. But really, none of this is needed in practice. The comment will probably trick the validator, but unless you're targeting Netscape 2.0 I don't think you actually need them. In any case, a better approach would be to use external scripts, which will clean up your page, and help with front-end performance.



Thanks for replying....

But can you tell me if i was right(see code above)
Christian Johansen
author
Ranch Hand

Joined: Oct 03, 2010
Posts: 46
Well, yes, it will work but it is not needed.
Jagdeep Sharma
Ranch Hand

Joined: May 24, 2010
Posts: 121

Christian Johansen wrote:Well, yes, it will work but it is not needed.


But i have seen this code which is different from my code please see it.




Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
Have you run your code through a validator? What does it say?

Eric
Christian Johansen
author
Ranch Hand

Joined: Oct 03, 2010
Posts: 46
I was actually a bit quick in my first reply. Your second example is the right one if you need the HTML comments. You really don't, unless you're prepared to support 10+ year old browsers. Also the whole CDATA thing only matters if your site is actually served as XHTML (i.e., with a Content-Type of "application/xhtml+xml"). I doubt you are doing this, as for example Internet Explorer does not support it at all until the upcoming version 9.

Here's the simplest way (I think) to trick the validator and be safe in browsers:

Jagdeep Sharma
Ranch Hand

Joined: May 24, 2010
Posts: 121

Christian Johansen wrote:I was actually a bit quick in my first reply. Your second example is the right one if you need the HTML comments. You really don't, unless you're prepared to support 10+ year old browsers. Also the whole CDATA thing only matters if your site is actually served as XHTML (i.e., with a Content-Type of "application/xhtml+xml"). I doubt you are doing this, as for example Internet Explorer does not support it at all until the upcoming version 9.

Here's the simplest way (I think) to trick the validator and be safe in browsers:




What if multi-line comments are not supported. I think you are incorrect. Please check your code and can you explain it.
Christian Johansen
author
Ranch Hand

Joined: Oct 03, 2010
Posts: 46
What browser doesn't support multi-line comments?
Jagdeep Sharma
Ranch Hand

Joined: May 24, 2010
Posts: 121

Christian Johansen wrote:What browser doesn't support multi-line comments?


how would you hide the javascript code from really old browser which don't understand javascript.
Christian Johansen
author
Ranch Hand

Joined: Oct 03, 2010
Posts: 46
As I've already said, doing so isn't necessary. But if you absolutely want to, your second example will work. However, keep in mind that if you're using a JavaScript library such as jQuery, your code will break in old browsers anyway. Not in those archaic browsers that don't support JavaScript at all, but slightly newer browsers such as IE 5.5. So unless you're including some very basic JavaScript, chances are that the comments will protect your site from crashing in e.g. Netscape 2, but the site will still (probably) fail in Firefox 1, IE 5.5, Safari 2 and others, for other reasons.
Jagdeep Sharma
Ranch Hand

Joined: May 24, 2010
Posts: 121

Christian Johansen wrote:As I've already said, doing so isn't necessary. But if you absolutely want to, your second example will work. However, keep in mind that if you're using a JavaScript library such as jQuery, your code will break in old browsers anyway. Not in those archaic browsers that don't support JavaScript at all, but slightly newer browsers such as IE 5.5. So unless you're including some very basic JavaScript, chances are that the comments will protect your site from crashing in e.g. Netscape 2, but the site will still (probably) fail in Firefox 1, IE 5.5, Safari 2 and others, for other reasons.


Actually i have to incorporate all the browsers. You are right today that is not neccessary. But i want to do it as an learning exercise. Can you explain how commenting works in following code


It will be great help.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Adding CDATA tag in script tag
 
Similar Threads
alert is not working
How to read a cdata using sax parser
Silly little Javascript problem but can't figure it out
  in text Node
JSF Chrome