XML technologies / CSS / Advanced websites with CSS / Miscellaneous CSS topics / CSS and JavaScript

(Excerpt from “CSS − Advanced websites with Cascading Stylesheets” by Rachel Andrew & Dan Shafer)

Combining XHTML, JavaScript and CSS produces what is called dynamic HTML, or DHTML. Many mistakenly consider DHTML a separate technology; it is merely a term highlighting the great potential for interactivity and dynamism in (X)HTML web pages.

Many books have been written about DHTML, including DHTML Utopia: Modern Web Design Using JavaScript & DOM, written by Stewart Langridge and published by SitePiont.

I will not try to teach JavaScript or DHTML here, since both subjects are far too large for this book. A brief overview will whet your appetite and answer persistent questions about using them with CSS.

At the heart of DHTML is the Document Object Model (DOM). To simplify somewhat, think of the DOM as a specification or definition of how to address individual website elements. It lets you change their appearance and behavior. JavaScript is the language generally used to express these instructions.

Essentially, JavaScript can reference and change any CSS property. In JavaScript, address an element by name or ID, specify the property to read or change, and assign a new value. The changed styles take effect as soon as the script runs.

For example, create a button that hides a particular part of the page content, whose ID here is schnell_weg. Activate it with the JavaScript function hideShow. It would look roughly like this:

function hideShow() {
  document.getElementById("schnell_weg").style.visibility="hidden";
}

Where to define this script, when and how to run it, and all further details are outside our scope here. The point is that JavaScript can access and change the styles of elements in an HTML page, even after the page has loaded in the browser. The syntax differs little from our example. Initially, typing getElementByID whenever you want to inspect or change an element’s style seems tedious. But because the operator is always the same for every instance, you can learn to handle the many different scripting requirements more quickly.

  

 

 

 

back to top