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

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 (30)

Comment Pages 3 of 3 pages « First  <  1 2 3

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
if you have to use javascript, keep it small. It always kill my browser.
Posted by dll download on 05/07, 06:20 PM
now, thats some good stuff, ty
Posted by Meet Women on 05/28, 12:52 PM
the surrounding SCRIPT tags. Comments within the script tags are still allowed.
I've already bookmarked your blog. baby names
Posted by baby names on 10/29, 10:51 PM
Dude.. I am not much into reading, but somehow I got to read lots of articles on your blog. Its amazing how interesting it is for me to visit you very often :)
Nintendo DSi Nintendo DS Eye
Posted by DSi on 11/10, 01:03 AM
Thanks for sharing this!
phoenix tattoos
Posted by phoenix tattoo on 11/12, 09:18 PM
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
This is one of the best I’ve recently read! I am not much into reading, but somehow I got to read on your blog for hours today =) Water Damage Restoration
Posted by Water Damage on 11/21, 03:09 AM
I remember arguing with my friend about this yesterday! Didn't imagine that I will run into this article :) . Wait till I forward this URL to him. Pretty sure he will have a good read. Thanks for sharing! Water Damage Restoration
Posted by Water Damage on 11/21, 03:11 AM
I am deeply in love with every single piece of information you post here. Will be back often to read more updates! Document Drying
Posted by Document Restoration on 11/27, 12:39 AM

Comment Pages 3 of 3 pages « First  <  1 2 3

Name: (Required)

Email: (Required)

(NOT shown on page)

URL:

(Shown on page if entered)

Comments: (Required)

Supported BB codes: [b], [i], [u], [em], [strike], [strong], [pre], [code], and [blockquote].

Remember my personal information
Notify me of follow-up comments?

Submit the word you see below:


Sponsors

Advertise Here

Copyright 2006-2008 Dynamic Drive Read our Usage Terms before using any of the CSS codes.
Dynamic Drive DHTML and CSS Code Library