When you're creating a Web page, it is important to keep the layers separate. As I pointed out in an earlier article, using external style sheets is the best way to separate your content from your design. And the same is true for using external JavaScript files.
Some of the benefits of separating the layers are:
The content layer is where you store all the content that your customers want to read or look at. This includes text and images as well as multimedia.
It's important that your content layer be as standards-compliant as you can make it, whether it's HTML or XHTML. That way, it will merge more easily with your style and behavior layers. I strongly recommend using valid XHTML (either transitional or strict) because that is the most up-to-date content layer technology and will set your site up to be usable well into the future.
When you use valid XHTML that provides clearer hooks into the content for your styles and behaviors to grab onto. For example, in HTML, the paragraph tag (<p>) doesn't require an end tag. Most of the time this isn't a problem, but some browsers may interpret the end of a paragraph in a different location than you expect. And when your styles are applied, your page will look wrong. Just using the end paragraph tag (</p>) tells the browser exactly where you expect the paragraph and any associated styles to end.
It's also important to make sure that every aspect of your site is represented in the content layer. That way, your customers who have JavaScript turned off or can't view CSS will still have access to the entire site, if not all the functionality.
Store all your styles for your Web site in an external style sheet. This defines the way the pages should look, and you can have separate style sheets for various media types.
Store your CSS in an external style sheet so that you can get the benefits of the style layer across the site.
JavaScript is the most commonly used language for writing the behavior layer, but as I mentioned before, CGI and PHP can also generate Web page behaviors. However, when most developers refer to the behavior layer, they mean that layer that is activated directly in the Web browser - so JavaScript is nearly always the language of choice. You use this layer to interact directly with the DOM or Document Object Model. Writing valid XHTML in the content layer is also important for DOM interactions in the behavior layer.
When you build in the behavior layer, you should use external script files just like with CSS. You get all the same advantages of using an external style sheet.