Selecting the correct background using JavaScript

Different web browsers use slightly different layout formats. For example the following test page works correctly with Netscape 4 but not with Netscape 3 (at least on and SGI). JavaScript can be used to select a different backgroud image if a particular type of browser is detected. Here we look for Netscape 3 or lower and select a different image using JavaScript. With Netscape 4, both of these example pages should work fine.

How to do this

The JavaScript used in the second example is:
<SCRIPT LANGUAGE="JavaScript">
  // Simeon Warner - 14Apr98
  // Here we are trying to find out what browser and version is being
  // used to correct inconsistencies between versions.
  browserName    = navigator.appName;
  browserVersion = parseInt(navigator.appVersion);
  if ( browserName == "Netscape" ) {
    if ( browserVersion < 4) {
      document.write ("<body background=\"red_back_netscape3.gif\">")
    }
  } 
  // All other browsers will pick up the background outside the script
  // tags. The second body definition will be ignored for browsers 
  // specified in this script (nasty but it works).
</script>
<body background="red_back.jpg" width=900 height=50
  bgcolor=#ffffff link="#330000" vlink="#555555">
Points to notice:

[ Up ]


This page is maintained by Simeon Warner
Last updated 14 April 1998