Document Output

Outputting to the current document is easy. Just use the methods write and writeln. Example:
  <SCRIPT LANGUAGE="JavaScript">
    <!-- Hide JavaScript from older browsers
      document.write(" This <a href=\"index.html\">link</a> was produced by JavaScript")
    // end hiding from older browsers -->
JavaScript is interpreted in real time. This means that any HTML appearing before this JavaScript code will appear first. Then the JavaScript code will be ran and the resulting string will be placed. Then any HTML after that will be displayed.

The method writeln is the same as write but a new line (carriage return, return character, whatever) will be placed at the end of the line.

If you have multiple frames or windows, you can "talk" to those other windows or frames. Just use the proper referencing. For instance, if we have two frames, one called frame1 and the other called frame2, you can send data between the two. If we have our JavaScript code in frame1 and wish to send something to frame2, then the following code will do the trick:

  <SCRIPT LANGUAGE="JavaScript">
    <!-- Hide JavaScript from older browsers;
      top.frame2.document.write("Hi there");
    // end hiding from older browsers -->
top specifies the top of the document heirarchy. The next argument is the name of the frame. After that you can specify any object within the frame. In this example, we use the document object. And finally we use the write method of the document object.

You may have noticed the open() and close() methods. These calls open a stream from one object to another. In other words you need to open the door to other objects before you can start talking to them. Just be sure to close the door when you're done. One sidenote regarding open() is that it will clear the target window. Don't forget that.

There are some other features for open(). open() by itself implies that you are opening a text/html document. This means you can only send over plain text (or HTML). If you wish to send something else, then you can specify that MIME Type. Some MIME types are image/gif, or image/jpeg. So if you open it with a gif type, then it will expect you to send a GIF image. Example:"image/gif");
For more information, check out a handy reference.

If you wish to change the URL location of another window (or the current window for that matter), you can use the location property of the document object. Example:

If you wish to load a document in a specific frame, just reference it appropriately.
Continue to the next lesson: Opening Windows and Frames.
Return to the Front Page.