Advanced Search Usage Terms Submit Contact
Dynamic Drive CSS Library
 
CSS Library
Submit an original CSS code
CSS Layouts
Web Graphics
Online Tools:
Last commented:

Dynamic Drive Blog Here

Updating a JavaScript to be XHTML compliant

Date: 05/14/2006 | By ddadmin | Comments: 22

As a webmaster you've undoubtedly noticed the increasing shift in coding practices from HTML to XHTML on the web. If you're a professional web designer, you've probably even heard it first hand from your clients who demand their web pages be XHTML compliant. I get quite a lot of emails on a monthly basis asking for help in making a webpage that contains a DHTML script to be XHTML compliant. A common misconception is that this is a difficult process, which can be no further from the truth.

There are two ways to go about making a DHTML or JavaScript XHTML compliant, both should take no more than a few moments of your time.

Method 1: For inline JavaScript, wrap the code itself in the CDATA tag coupled with some clever use of JavaScript comments to let the validator know the content within the script should not be parsed for XHTML validity:

<script type="text/javascript">
/*<![CDATA[*/
//The JavaScript code itself
//The JavaScript code itself
// etc...
/*]]>*/
</script>

As you can see, just wrap any inline JavaScript with the parts in red, and you're done. You may also choose to use an alternate version of the CDATA/ JavaScript comment code:

<script type="text/javascript">
//<![CDATA[
//The JavaScript code itself
//The JavaScript code itself
// etc...
//]]>
</script>

Either code wrappers work.

Method 2: The other method for making your JavaScript XHTML compliant is to remove the entire script from your page, and place its contents inside an external JavaScript file, then reference this file on your page:

<script type="text/javascript" src="myscript.js">
//Comment here
//Comment here
</script>

Where "myscript.js" contains the script itself minus the surrounding SCRIPT tags. Comments within the script tags are still allowed.

And there you have it. Now a JavaScript no longer has to trip up the XHTML validity of your pages!

Your Comments (22)

Comment Pages 2 of 3 pages  <  1 2 3 >

We had several issues when we upgraded our app.Thanks
Posted by anoop on 05/18, 02:37 AM
Method 2 worked
Posted by Prem Kumar on 05/18, 02:45 AM
This has made it easy i was getting it wrong some where i had a problem with code wrapper work all this "contents inside an external JavaScript file"

Its now easy to implement Thanks
Posted by DotWhoo on 06/05, 02:28 PM
For some reason this method doesn't seem to work for me. I will check it once again and update again
Posted by Rajesh varma on 06/06, 10:01 PM
Blog giving nice information
Posted by Easyrules on 10/18, 04:17 AM
@Edu Guru: The difference is that in both versions of method 1 the CDATA part is commented out, but JavaScript has two different styles of commenting code.

If you use the style in the second example, you'd have to be careful to not put anything else on the line along with that CDATA or ]] code, because the two slashes mean the entire line is commented out. I think using the first version is a tiny bit "safer."
Posted by Jenn on 11/01, 01:41 PM
Very good.I did'n know it before i've read this article.
I will use this approach in my projects ;)
Posted by Drawit on 05/04, 02:24 AM
I admire the valuable information you offer in your articles. I will bookmark your blog and have my children check up here often. I am quite sure they will learn lots of new stuff here than anybody else!
Casa a New York
Posted by Loft a New York on 11/12, 10:47 PM
There is a problem of loading the correct page while clicking on the link XHTML compliant. Will you please check the matter?
Posted by Shruti on 12/18, 01:16 AM
Please note that the <script> tag must be in lowercase, and not uppercase, in order to validate with XHTML. I spent a lot of time until I realised that :-)
Posted by Marathe on 12/28, 10:23 AM

Comment Pages 2 of 3 pages  <  1 2 3 >

Commenting is not available in this weblog entry.
Sponsors

Advertise Here

Copyright 2006-2011 Dynamic Drive Read our Usage Terms before using any of the CSS codes.