Writing JavaScript-Friendly HTMLBecause you'll be using JavaScript to manipulate the objects within a document, you want to write your HTML in a way that can be easily used by your scripts. That basically means writing modern, standards-compliant XHTML and using CSS to separate the document's structure from its presentation. When we say modern XHTML, we don't just mean documents that pass W3C validation using the Web tool at validator.w3.org. We also recommend thinking ahead to what you are likely to do with a page and adding appropriate tags and attributes that will make it easy to access objects with JavaScript. What sort of markup, you wonder? Glad you asked. Structure, presentation, and behaviorCSS (Cascading Style Sheets) is a standard layout language for the Web that controls typography, colors, and the size and placement of elements and images. Your XHTML documents should have external style sheets defining the styles used within the document. Your JavaScript should also be in an external document, one that contains only JavaScript code. When split up this way, your sites will contain three types of text files:
When you do this, it becomes straightforward to make changes to your siteeven changes with site-wide effects. Divs and spansIf you're used to the classic style of HTML, where you threw everything into tables and played with spacer GIFs until it all fell into more-or-less the layout you wanted, some of this will be new to youso here's a quick overview to bring you up to date. XHTML contains two tags that are just starting to get the attention they deserve: <div> and <span>. They're used to break up your content into semantic chunks, that is, chunks that have a similar meaning. Things inside a single table cell or paragraph may or may not have anything in common, but the content within each <div> and <span> should. Why use one over the other? A <div> is a block-level element, that is, there's a physical break between it and the elements above and below it. A <span> isn't block-level; instead, it's inline, so you can apply it to, for instance, a single phrase within a sentence. We're not saying that you need to junk everything you've ever learned about HTMLfar from it! But add these two tags to your toolkit of solutions, and you'll be surprised how often you use them. Classes and idsInside your XHTML document, you'll mark up your content by breaking it down into those meaningful chunks. From there, you'll still need to identify those pieces of content where you want to change their presentation or behavior. For that, you'll primarily use two attributes: class and id. These attributes can be used by both CSS and JavaScript; a CSS style sheet uses those attributes as part of rules to define the appearance of a page, and the JavaScript file can use those attributes in code that affects the behavior of elements on the page. Tip
|