Chapter Thirteen
Reading Browser Memory


Specialized Things You Can Know about Your Visitors Using JavaScript

It will be useful on a number of occasions to be able to identify the kind of browser that your visitor is using.

This can be done with a number of different navigator.code calls:

navigator.appCodeName

This is a call that allows you to read (only) the code name of the browser.

Despite the fact that "Mozilla" is a code name that Netscape® used first, caution should be exercised when relying upon this call to let you differentiate between Netscape® and Microsoft® browsers, since some of the latter also say "Mozilla" or "Mozilla (Compatible)". Aggressive Innovative aren't they?

str=navigator.appCodeName;
// str now equals "Mozilla"
str1="The browser you are using has a code"
+" name of ";
document.write(str1+str);
// or you may use the following:
document.write("The browser you are "
+"using has a code name of "
+navigator.appCodeName);

One caution: some early versions of Netscape® browsers in the 2.x series had a "bug" that "erased" the name after you read it just once since your visitor last started his/her browser application. So, if a prior page visited also read this value, your "read" of it would return a null value. This bug is fixed in the 3.x series.

The "read (only)" means that you can't assign a different name to the various navigate.code calls. You'll get an error message if you try.

navigator.appName

This is a call that allows you to read (only) the name of the browser.

This call will return "Netscape" for Netscape browsers only, so is a safer call to rely upon for determining browser type.

str=navigator.appName;
// str now equals "Netscape"
str1="The browser you are using has the"
+" name of ";
document.write(str1+str);
document.write("The browser you are "
+"using has the name of "
+navigator.appName);

navigator.appVersion

This is a call that allows you to read (only) the application version of the browser.

The information will be returned in the following format:

releaseNumber(platform,country)

releaseNumber is the version number of the browser. For example, "3.0b5" specifies Navigator 3.0, beta version 5.

platform is the computer on which the browser is running. "Win16" tells you it is a 16-bit version of Windows® such as Windows® 3.xx, "Win 95" the 32-bit version of Windows® 95, "Mac 68" and "MacPPC" for the Macintosh® standard and Power PC units.

country is either "I" for the international release, or "U" for the domestic U.S. release. (The domestic release has a stronger encryption feature than the international release)

str=navigator.appVersion;
/* str now equals "3.0 (Win95, I" for a 3.0
  browser on Windows 95® machines,
  "2.0 (Mac68k, I", etc. */
str1="The browser you are using has the"
+" version name of ";
document.write(str1+str);
// Or you may use the following:
document.write("The browser you are "
+"using has the version name of "
+navigator.appVersion);

One of the most usual reasons you will want to call the navigator.appVersion is to allow you to know what new line and/or carriage return characters to use for displaying information in pre-formatted HTML or in text areas on screen.

navigator.appVersion will always contain "Win" in the returned string when your visitor is using Windows®.

If the user is running Windows, the newline character is set to "\r\n". For UNIX and Macintosh® machines, it is set to "\n".

<SCRIPT LANGUAGE="JavaScript">
<!-- Hide from JS-Impaired Browsers
var rr="\n";
if (navigator.appVersion.IndexOf('Win')>-1)
 rr="\r\n";
 }
// End Hiding -->
</SCRIPT>

A slightly more compact way of doing the same thing:

<SCRIPT LANGUAGE="JavaScript">
<!-- Hide from JS-Impaired Browsers
rr=navigator.appVersion;
rr=((rr.IndexOf('Win')>-1)?"\r\n":"\n");
// End Hiding -->
</SCRIPT>

navigator.userAgent

This is a call that allows you to read (only) the complete user-agent header sent in the HTTPd protocol from the visitor to the server (used by the server to partially identify the visitor)

You may elect to use this call for determining browser in lieu of the appVersion if you wish:

str=navigator.userAgent;
/* str now equals "Mozilla/3.0 (Win16, I"
  for a 3.0 browser on Windows® 3.xx
  machines, "Mozilla/2.0 (Mac68k, I", etc. */
str1="The browser you are using has the"
+" user-agent name of ";
document.write(str1+str);
// Or you may use the following:
document.write("The browser you are "
+"using has the user-agent name of "
+navigator.userAgent);

document.lastModified

This is one of those "automatic" kinds of calls you can imbed at the bottom of a document, for example, to "take care of" the terribly onerous chore of typing a new date there whenever you change the document.

Good idea in principle, but subject to some problems for your web site. First, this will not change the header info on that document, so folks who poll your site for return visits based upon changes may not always be clued that an update has occurred.

Second, it is extremely unpredictable for local displays on different platforms. Macintosh®, for example, because its time function uses 1900 instead of the "epoch" year of 1970 generates a huge number of milliseconds (needed to generate the date modified) which in turn results in displays of dates in the late 1920's, long before the World Wide Web was a gleam in ENIAC's eye!

You do as you wish. Personally, I don't use this call, since if I'm modifying a document anyhow, why not just change that date at the same time? Means a whole lot fewer bytes to download to every visitor.

Caution: This will not work properly if you do not type "lastModified" with the lower case "l" and upper case "M".

But if you want to use it, put this little script where ever you want the message to appear in your document. Because a different date than the Epoch is used for Macintosh computers, a little test is needed:

<SCRIPT LANGUAGE="JavaScript">
<!-- Hide from JS-Impaired Browsers
document.write("This page last updated"
+" on ")
if (location.href.substring(0,8)=="file:///"
&&navigator.appVersion.indexOf("Macintosh")>-1){
 nr=2212122499000; // Macintosh offset
 lm=document.lastModified;
 dt=new Date(lm);
 nr+=dt.getTime();
 cdt=new Date();
 cdt1=cdt.setTime(nr);
 document.write(cdt);
 }
else{
 document.write(document.lastModified);
 } // End Hiding --> </SCRIPT>

document.title

This call lets you "read" the title of the present document from browser memory.

It falls into that category of things you can do (just because you can) but probably never will.

Why's that? Well, if, for example, you opened a new window to display someone else's document, the title of that document will be displayed on screen anyhow (at the top of the browser window) and if you're opening your own document, it's a pretty sure bet you already know the title - and even if for some obscure reason you don't, it's displayed at the top of the browser window, too!

You can only read the content appearing between the <TITLE> and </TITLE> tags. For security reasons, you can't write it or assign a new value to it. (Think about it and you'll see why)

document.write("The title of this "
+"document is "+document.title);

or

str=document.title;

document.referrer

This call is probably absolutely worthless to you except on your Home Page. Unless you are wanting to assess navigation by visitors within your site itself and have combined some .cgi to read it all when the visitors leave your site.

This call tells you the full URL of the page visited immediately prior to your implementation of this call.

A Script Using These Calls

This little script incorporates a number of the browser memory calls so that you can see how they work in practice:

<HTML><HEAD>
<TITLE>Reading Browser
 Memory</TITLE></HEAD>
<BODY BGCOLOR="white">
<CENTER>
<P><B>JavaScript Calls to Read Browser Memory</B>
<P><SCRIPT LANGUAGE="JavaScript">
<!-- Hide from JS-Impaired Browsers
document.write('<TABLE BORDER=0 WIDT'
+'H=486><TR><TD>This browser\'s <B>na'
+'vigator.appCodeName</B> is '
+'<FONT COLOR="blue">'
+navigator.appCodeName+'</FONT>'
+'<P>This browser\'s <B>navigator.app'
+'Name</B> is <FONT COLOR="blue">'
+navigator.appName+'</FONT>'
+'<P>This browser\'s <B>navigator.app'
+'Version</B> is <FONT COLOR="blue">'
+navigator.appVersion+'</FONT>'
+'<P>This browser\'s <B>navigator.use'
+'rAgent </B>is <FONT COLOR="blue">'
+navigator.userAgent+'</FONT>'
+'<P>Without correction for platform:'
+'<BR>This <B>document.lastModified</B>'
+' is <FONT COLOR="blue">'
+document.lastModified+'</FONT>');
document.write('<P><FONT COLOR="r'
+'ed">With test and correction for plat'
+'form (Macintosh run locally does not us'
+'e Epoch date of January 1, 1970, but th'
+'e turn of the century):</FONT><BR>Thi'
+'s <B>document.lastModified</B> is <FON'
+'T COLOR="blue">');
if (location.href.substring(0,8)=="file:///"
 &&navigator.appVersion.indexOf("Macintosh")>-1){
 nr=2212122499000; // Macintosh offset
 lm=document.lastModified;
 dt=new Date(lm);
 nr+=dt.getTime();
 cdt=new Date();
 cdt1=cdt.setTime(nr);
 document.write(cdt);
 }
else{
document.write(document.lastModified);
}
document.write('</FONT></TD></TR></T'
+'ABLE>');
// End Hiding -->
</SCRIPT>
</BODY>
</HTML>

Click Here to See Script.



© Copyright 1997, John H. Keyes john.keyes@intellink.net