Dynamic Documents
It's often useful to generate web pages (dynamic documents) from
Javascript code rather than from static files.
Create dynamic documents with document.write().
This method can:
- insert text into the current web page (as seen earlier)
- create an entirely new document (what this chapter is about)
Here's a sample page
that simply displays today's date.
Here's the source code.
Discover:
Compare the original source code to what you see when you View Source
in the browser itself.
Lab
Your task:
- Create a web page containing a form containing a button.
- When the button is clicked, generate a dynamic web page.
- Hint: onClick invoke document.write().
Solution
Don't peek unless you're stuck!
Extra for Experts
- Instead of replacing the web page in the existing
browser window, open a new window and load the dynamic
page into it. Hint: use the Window.open() method.
- Here's a "cleaner" version
(and its source code) of the
example program shown at the beginning of this chapter...
but it's got a bug. Explain.