Interacting with the web browser

Unfortunately, JavaScript can be used to annoy. We have all suffered the continual popping-up of new windows without the usual toolbars (these are known as consoles), changes in the browser appearance, and resizing of the browser.

Having said that, adding features that are helpful is desirable. Example 7-6 shows four examples of handlers for buttons that use methods or functions defined for the browser window object. The function window.close( ) closes the focused window, window.print( ) shows the print dialog window, windows.history.go(-1) goes back one page, and ) opens a new browser window.

Example 7-6. Closing and opening windows with JavaScript, printing the current page, and adding a Back button to a <form>
              "-//W3C//DTD HTML 4.01 Transitional//EN"
  <title>Playing with the Browser and Windows</title>
<h1>Playing with the Browser and Windows</h1>
<form action="example.7-6.php">
  <input type="button" value="Close Window"
       onClick="window.close(  );">
  <br><input type="button" value="Print Window"
      onClick="window.print(  );">
  <br><input type="button" value="Go Back"
  <br><input type="button" value="Visit the my web site"

The page rendered in a Netscape browser is shown in Figure 7-4.

Figure 7-4. Controlling the browser behavior through buttons

Only ) has any complexity. The first parameter is the URL to request in the new window, the second is a title, and the third is a set of properties the new window has. Without the list of properties that are included, the default new window has no Location box, no toolbars, no scrollbars, and can't be resized: it's an evil console!