JavaScripts
Replace Window
Grizzly WebMaster home
 Grizzly WebMaster (HOME) >> JavaScripts >> Replace Window
It is often desirable to replace the contents of the window with another document.   This is useful when the Page Developer does not want the original page left in the browser page-history.

This version of "replaceWindowURL" uses an often under-utilized capability of JavaScript to allow for two or more definitions of the same function.  If a function is defined in a JavaScript code-block, that is redefined in a later code-block the later function will be used.  The trick is that the later code-block is a "versioned" JavaScript code-block.  That way you have a function that is defined for "all" JavaScripts and one (or more) that is defined for a specific version of JavaScript.

Notice that one version does not actually "replace" the window contents but merely loads a new page (JavaScript).  The second version of the function (in JavaScript1.1) actually replaces the window contents.

It is important that the JavaScript1.1 version follows the JavaScript version.

Obviously before using these functions you would want to remove the verbose comments.


<script language="JavaScript">
<!--
//  This function will not "replace" the page in the browser history
//  but is provided for those browsers that did not support the
//  "replace" method.
//
function replaceWindowURL( win, url )
{
    win.location.href = url;
}
//-->
</script>
<script language="JavaScript1.1">
<!--
//  This function uses the "replace" method on the location object
//  that was added in JavaScript 1.1
//
function replaceWindowURL( win, url )
{
    win.location.replace( url );
}
//-->
</script>
<script language="JavaScript">
<!--

function doLoad()
{
    //
    //  This is an exmaple of calling "replaceWindowURL"
    //  to load the page "redirectiontarget.htm"
    //
    replaceWindowURL( window, "redirectiontarget.htm" );
}

//-->
</script>
See the following article for examples of using replaceWindowURL:
Service Provided by Bear Consulting Group Last modified: 25 May 2009
Copyright 2001 Bear Consulting Group