The Hello Viewer Sample

The Hello Viewer sample, installed with the Developer’s Guide samples, shows simple examples of using the Viewer API from different parts of a web layout.

The tool bar for the sample contains a custom Invoke Script command that calls the ZoomToView() function of the mapFrame. This is executed in the context of the main frame, so the function is available using

    mapFrame.ZoomToView()

The task pane loads a page that shows two other ways of calling ZoomToView(). One way loads a custom page into the hidden scriptFrame. The page reads GET parameters and passes them to the JavaScript function call. This is executed in the context of the scriptFrame, so ZoomToView() is available using

    parent.mapFrame.ZoomToView()

Another way calls ZoomToView() directly when a link is clicked, using the JavaScript onclick event. This is executed in the context of the taskPaneFrame, so ZoomToView() is available using

    parent.parent.mapFrame.ZoomToView()

The Developer’s Guide samples also demonstrate a more advanced method for using JavaScript in a Viewer. The file index.php includes an external JavaScript file that solves 2 problems:

- When a map is first loading, the task pane displays before the map has been fully initialized. This can cause problems if users click any links in the task pane that depend on the map being available.
- The first time the Viewer loads a page into the task pane, it passes SESSION and WEBLAYOUT as GET parameters. The map name is not known until after the web layout has loaded. When a user clicks the Home button, the Viewer reloads the home page in the task pane, but passes SESSION and MAPNAME as GET parameters instead. In some cases, it may be useful for the home page to have the map name when it first loads.

To deal with these problems, the Hello Viewer sample loads pageLoadFunctions.js, which attaches a function to the window.onload event of the page in the task pane. This function does the following:

- Replaces the OnMapLoaded() function of the main frame. This function is called after the map has been fully initialized. The new version performs some initialization (see below), then calls the original OnMapLoaded().
- Saves the contents of the task pane page and replaces it with the text “Loading...”.
- After the map is fully initialized, it calls the new version of OnMapLoaded(). At this point, the map name is known, and is available from the mapFrame.GetMapName() function. The new version of OnMapLoaded() restores the contents of the task pane page, then it searches all <a> elements, replacing “MAPNAME=unknown” with the correct map name in the href attributes.

See the Hello Viewer sample for links to view index.php and pageLoadFuctions.js.