JavaScript and XHTML

106 10
< Continued from page 2

In writing JavaScript to work with HTML you have three different ways that you can write your JavaScript so as to interact with the web page.
  • You can use document.write() to output directly into the HTML as the page is being created.
  • You can use innerHTML to reference and update the content of any element irrespective of what other elements it contains.
  • You can use the HTML DOM to interact with individual elements within the web page.



    While the first of these ways only works before the page finishes loading and is therefore only useful for the sorts of processing better done using server side languages, the second option actually provides a simple way of performing a lot pf processing that would otherwise require far more code in order to get it to function correctly if you did it using the DOM.

    XHTML has far fewer alternatives when it comes to how your JavaScript can interact with the web page. In fact there is only one way that JavaScript can interact with XHTML.
    • You can use the XHTML DOM to interact with individual elements within the web page.

    So when writing JavaScript that needs to be able to interact with XHTML you can forget all about such things as document.write and innerHTML since those options simply don't exist so far as XHTML is concerned. The only way for JavaScript to interact with XHTML is via the DOM and even there the DOM that you need to reference isn't the same as the HTML DOM. The actual differences in how you use them are relatively minor though and so rewriting code that currently uses the HTML DOM to use the XHTML DOM is a relatively simple task - certainly a lot simpler than trying to rewrite code using either of the other two HTML ways of interacting with the page.

    So if you are currently writing HTML and JavaScript to work with that HTML and expect subsequently to switch to using XHTML then using the DOM is the best way to set up all of your web page interactions as that will make it much simpler to convert your code to work with XHTML.
    Source...
    Subscribe to our newsletter
    Sign up here to get the latest news, updates and special offers delivered directly to your inbox.
    You can unsubscribe at any time

    Leave A Reply

    Your email address will not be published.