![]() ![]() ![]() ![]() |
![]() |
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)". 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. 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 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) 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". A slightly more compact way of doing the same thing: 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: 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: 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) or 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: |