A series of examples that rewrite the document space:
| working example | explanation | excerpt of relevant code |
| simple with button | A new page is written containing a button. | function morning(){
document.write("<html><p><b>Hello!</b></p>") document.write("<form><input value='ok' type=button onclick='history.go(-1)'> </form></html>") alert(s); } |
| In the above example, the link calls the function 'morning'
which writes a new html page. The <html> and </html> tags are not strictly
required, but serve to remind us that, indeed, it is html code which is being written
within the string output by document.write. The page written contains a wee bit of text:
"Hello!" and a button which, when clicked takes the user back
to the previous version of the page, using 'history.go(-1)'. |
||
| link to working example | explanation | excerpt of relevant code |
| scripts/documentwrite.html | illustration of synchrony | document.write("Here we go<p><b>"); function clicked(){ document.write("The button has been clicked<p>"); document.write("<input type='button' value='click here' onclick='history.go(-1)'></form>"); } |
| In the above example, note that the words "Here we
go" are displayed on the page when it first loads into the browser window. The
paragraph tag <p> as well as the begin boldface <b> are still in effect when
the body of the document (which begins "<body>Hello") loads immediately
thereafter. The commands within the function are not carried out until the button is
pushed. Once the button is pushed the document is rewritten. You will note that although
the boldfacing was never "turned off" (with </b>) it is no longer in
effect when the document is rewritten. |
||
| link to working example | explanation | excerpt of relevant code |
| something which reuses user input in the new page -- not for Netscape | function thescript(){ userstring=document.forms[0].whatever.value; document.write("here's what you typed, but new and improved:<br>" ); document.write("<b><i>"+userstring+"</i></b>"); } |
|
| When the function is activated, the user's input is embedded into the html, boldfaced and italicized. Instead of referring to the form by name, we've referred to it by its number (zero) in the "forms" array. | ||
| link to working example | explanation | excerpt of relevant code |
| Same as above but working better in Netscape. | function thescript2( ){ userstring=document.forms[1].whatever2.value; document.write("<html><head><script>var userstring='"+userstring+"';</scr"); document.write("ipt></head><p>here's what you typed, but new and improved:" ); document.write("</p><p><b><i>"+userstring+"</i></b></p>"); } |
|
| The earlier script has the problem that the first document.write erased the browser's memory of the value of "userstring." By including this variable in the very first document.write (while it's value is still known), we can overcome that problem. Note, also the break-up of the string "/script" into two pieces -- this overrides Homesite's inclination to take the string as a signal to end the script. | ||