Netscape DevEdge

Skip to: [content] [navigation]

A Window-based Document Loader

Introduction

Loading documents either as components of a web application or for use in pre-fetching requires reliable notification of when the documents are available. WDocumentLoader provides an easy to use, cross-browser means of loading documents which works in Netscape 4.x, Netscape 6, Netscape 7, Mozilla, all Gecko-based browsers, Internet Explorer and Opera™.

WDocumentLoader.html extends a normal window object with several properties and functions which can be used inside an IFRAME or customized to be the main page of a web application.

WDocumentLoader.html uses CFormData to handle processing of the query strings used to specify the documents to be loaded.

Synopsis

Properties
document.location.search

document.location.search contains the query string used to specify the files to be loaded. Each file is specified as the value of a file variable as in:

?file=URL1&file=URL2&...&file=URLn

To load a set of documents using WDocumentLoader, simply load WDocumentLoader.html?file=URL1&file=URL2&...&file=URLn in an external window, FRAME or IFRAME.

Note that additional information can be passed via the query string.

Boolean gDataLoaded

gDataLoaded is set to true when the onload event handler is called for the FRAMESET containing the documents to be loaded.

CFormData gFormData

gFormData is an instance of the CFormData JavaScript object which is used to process the query string specifying which documents are to be loaded.

Functions
handleDataLoad

handleDataLoad is called by when the load event fires for the the FRAMESETs. It sets the global variable gDataLoaded to true, and calls the WDocumentLoaderCallback function defined in either the parent or opener window if it exists.

June 13, 2003 update It is now possible to pass the name of the callback function to the WDocumentLoader in the form:

?callbackfunction=functionName&file=URL1&file=URL2&...&file=URLn
outputFrameset()

outputFrameset() uses document.write to output a FRAMESET containing a FRAME for each file specified in the query string used to load WDocumentLoader.html.

getWindowByIndex(Number aIndex)

getWindowByIndex() returns a reference to the aIndexth loaded window.

getWindowByFileName(String aFileName)

getWindowByFileName() returns a reference to the loaded window containing file aFileName.

Example 1

Load a single file.

var documentLoader;      

function WDocumentLoaderCallback()
{
  var message = 'Loaded: ';
  var files = documentLoader.gFormData.getValue('file');

  // if there is only one value, it is return as a simple
  // value. Since we are reusing this function to handle both
  // single values and multiple values, we convert a single value
  // back to an array for exposition purposes

  if (typeof(files) == 'string')
  {
    files = [ files ];
  }

  for (var i = 0; i < files.length; ++i)
  {
    if (i != 0)
    {
      message += ', ';
    }
    message += files[i];

    // compare returned window values
    if (documentLoader.getWindowByIndex(i) != documentLoader.getWindowByFileName(files[i]))
    {
      alert('there has been an error in getWindowByIndex/getWindowByFileName');
    }
  }
  alert(message);
  documentLoader.focus();
}

function doExample1()
{
  var querystring = 'WDocumentLoader.html?file=http://devedge.netscape.com/';
  documentLoader = window.open(querystring, 'example', 'height=500,width=500,status=1,resizable=1');
}

This example works in Netscape 6.x, Netscape 7.x, Mozilla, all Gecko-based browsers, Internet Explorer and Opera 7.

Note that Netscape Navigator 4.x does not load a single document however does load more than one reliably.

Example 2

Load two documents.

function doExample2()
{
  var querystring = 'WDocumentLoader.html?file=http://www.netscape.com/&amp;file=http://www.cnn.com/';
  documentLoader = window.open(querystring, 'example', 'height=500,width=500,status=1,resizable=1');
}

This example works in Netscape 4.x, Netscape 6.x, Netscape 7.x, Mozilla, all Gecko-based browsers, Internet Explorer and Opera 7.

Example 3

Load two documents.

function myCallback(formData)
{
  alert('My Callback says ' + formData.getValue('file'));
}

function doExample3()
{
  var querystring = 'WDocumentLoader.html?callbackfunction=myCallback&amp;file=http://www.netscape.com/&amp;file=http://www.cnn.com/';
  documentLoader = window.open(querystring, 'example', 'height=500,width=500,status=1,resizable=1');
}
A+R