Application Techniques



Opening and Closing Windows Using JavaScript

How to open a page in new browser window and specify the features of that window.

About this technique

Details

Category

HTML Client Techniques> JavaScript

Description

You'll learn about:

You can run this technique code from:

NOTE   First make sure that database is running on your localhost SilverStream Server

Related reading

See the chapter on advanced page topics in the Programmer's Guide

This example uses the agScriptHelper utility object's various openWindow() methods. The agScriptHelper methods perform tasks that are similar to JavaScript functions, saving you from having to write JavaScript code directly (such as when you need to manipulate browser windows).

Getting user-specified window features   Top of page

The following code shows the features() method, which returns a string that specifies the features of the window to open. The string is used by AgpScriptHelper's openWindow() method.

The controls menubar, toolbar, resizable, status, and scrollbars are checkbox controls. The code uses getState() to determine if each checkbox is set or cleared. It uses the features variable to contain the concatenation of all checkbox states. The features() method returns the features variable, which specifies the type of window to open.

  public String features() 
  { 
     String features = "width=550,height=275"; 
      
     if (menubar.getState()) 
     { 
        features += ",menubar=yes"; 
     } 
     else 
     { 
        features += ",menubar=no"; 
     } 
     if (toolbar.getState()) 
     { 
        features += ",toolbar=yes"; 
     } 
     else 
     { 
        features += ",toolbar=no"; 
     } 
     if (resizable.getState()) 
     { 
        features += ",resizable=yes"; 
     } 
     else 
     { 
        features += ",resizable=no"; 
     } 
     if (status.getState()) 
     { 
        features += ",status=yes"; 
     } 
     else 
     { 
        features += ",status=no"; 
     } 
     if (scrollbars.getState()) 
     { 
        features += ",scrollbars=yes"; 
     } 
     else 
     { 
        features += ",scrollbars=no"; 
     } 
   
     return features; 
      
  } 

Notes about the code

Using openWindow(url, window, features)   Top of page

The following code opens a window when the user clicks the first button (Button1). It calls the features() method to get the user specifications for the window's features. It then calls the 3-argument openWindow() method on the agScriptHelper object, which is an instance of the AgpScriptHelper class. This is a utility class that provides methods that create JavaScript code so that you can access certain JavaScript functionality without having to know JavaScript. This instantiation occurs automatically with each page.

  private void handle_Button1_pageActionPerformed(ActionEvent evt) throws Exception 
  { 
     String features = features(); 
     agScriptHelper.openWindow("pgWindowToBeOpened.html", "new", features); 
  } 

Notes about the code

Using openWindow(url, window, features, params)   Top of page

The following code opens a window when the user clicks the second button (Button2). It calls the 4-argument openWindow() method on the agScriptHelper object, which is an instance of the AgpScriptHelper class. The additional parameter is params, which is a Hashtable of parameters to send with the request to open the window. The displayed page (that is, the document that the client gets) will extract and use the parameters as appropriate.

  private void handle_Button2_pageActionPerformed(ActionEvent evt) throws Exception 
  { 
     String features = features(); 
      
     Hashtable params = new Hashtable(); 
     params.put("param1", "ONE"); 
     params.put("param2", "TWO"); 
     params.put("param3", "THREE"); 
      
     agScriptHelper.openWindow("pgWindowToBeOpened.html", "new", features, params); 
  } 

Notes about the code

Using openWindow(url, window, features, query, orderby)   Top of page

The following code opens a window when the user clicks the third button (Button3). It calls the 5-argument openWindow() method on the agScriptHelper object, which is an instance of the AgpScriptHelper class. The parameter query specifies the rows (of the data bound to the page) to display in the opened window. The parameter orderby specifies the display order of the rows. In this example, it is set to NULL.

  private void handle_Button3_pageActionPerformed(ActionEvent evt) throws Exception 
  { 
     String features = features(); 
     agScriptHelper.openWindow("pgWindowToBeOpened.html", "new", features, 
        "contacts.id > 0", null); 
  } 

Notes about the code

Applying request information to the page in the window   Top of page

The following code shows the pageRequestBegin event on the page pgWindowToBeOpened. This code runs immediately after the page is first loaded and at the beginning of every subsequent page request. It attempts to get specific data from the servlet request and uses them appropriately.

For example, if the user clicked on the second button in the pgWindowOpenClose page, the 4-argument openWindow() method executes with a Hashtable as one of the parameters. The following method is able to get the Hashtable's values to display them in text fields. If, however, the user clicks the first button, the 3-argument openWindow() method executes, and there will be no Hashtable values to extract from the servlet request.

  //Method in pgWindowToBeOpened 
   
  protected void pageRequestBegin(AgiHttpServletRequest req, AgiHttpServletResponse res) throws Exception 
  { 
     String[] values = req.getParameterValues("param1"); 
     if (values != null) 
        Label1.setText(values[0]); 
     else 
        Label1.setText(""); 
      
     values = req.getParameterValues("param2"); 
     if (values != null) 
        Label2.setText(values[0]); 
     else 
        Label2.setText(""); 
      
     values = req.getParameterValues("param3"); 
     if (values != null) 
        Label3.setText(values[0]); 
     else 
        Label3.setText(""); 
   
     String queryString = req.getQueryString(); 
     System.out.println("QUERYSTRING = " + queryString); 
     if (queryString != null) 
     { 
        if (queryString.indexOf("query") >= 0) 
           agData.gotoFirst(); 
        else 
           agData.clearRows(); 
     } 
     else 
        agData.clearRows(); 
  } 

Notes about the code

Closing the window   Top of page

The following code closes the window when the user clicks the Close window button in the pgWindowToBeOpened page.

  //Method in pgWindowToBeOpened 
   
  private void handle_close_pageActionPerformed(ActionEvent evt) throws Exception 
  { 
     agScriptHelper.closeWindow(); 
  } 

Notes about the code






Copyright © 2000, SilverStream Software, Inc. All rights reserved.