Viewers: in countries Watching now:
- Creating variables, functions, and loops
- Writing conditional code
- Sending messages to the console
- Working with different variable types and objects
- Creating and changing DOM objects
- Event handling
- Working with timers
- Building smarter forms
- Using regular expressions
And it doesn't look so bad, but there are a few issues with this. See document.write doesn't work in XHTML, it doesn't understand the DOM properly, and it just spits out stuff wherever it's placed. It's a clunky inflexible way of doing anything with a page. And the most substantial reason that you don't want to get used to it is because you can only use it when the page is initially loading. If, for example, I decide to have a function and put document right inside that function and then I'm going to just call that function a couple of seconds after the page has loaded, let's see what happens. So I save that.
I flip over to Firefox where we have it. I refresh this page and two seconds later, the document.write destroys the original page, because it will only work as the page is being created for the first time. Run it anytime after the page is loaded, it will erase the contents of the document, so much better to use innerHTML, or better still, to work with creating and manipulating the individual DOM elements and text nodes. Next step is if you ever see code that looks something like this, browser-sniffing code. You're reading something that seems to be detecting Netscape or Microsoft Internet Explorer or a particular platform.
Next step is the EVAL function. EVAL is a powerful little function that can take an expression and execute it as code. That sounds a little weird, so let me show you what I mean. Let's say I create a variable a that's a string and it contains kind of half a statement here, and variable b contains hello, and variable c contains a closing single quote and closing parentheses. What we can actually do is use EVAL and then we could combine those three variables together into one long string. And if I pass it to EVAL function, EVAL will actually cause that to be executed as if it was code.