Here is the javascript that is used on our main page to identify your browser.
<SCRIPT LANGUAGE="JavaScript">
<!--Engage Cloaking Device
document.write("<CENTER><H2>Information</H2>");
document.write("Code name: " + navigator.appCodeName + "<BR>");
document.write("Application name: " + navigator.appName + "<BR>");
document.write("Version: " + navigator.appVersion + "<BR>");
document.write("UserAgent: " + navigator.userAgent + "<BR><BR>");
document.write("According to the above information, ");
if (navigator.appName == "Netscape")
if (navigator.appVersion.lastIndexOf('Win') != -1)
{
document.write("you are using the Windows version of Netscape.<BR>");
}
else
{
document.write("you are using the Macintosh, Unix or another version of Netscape.<BR>");
}
else
{
document.write("you are using Microsoft Internet Explorer.<BR>");
}
document.write("<P></CENTER>");
//Disengage Cloaking Device -->
</SCRIPT>
Now, let's take this line by line and see what it does.
<SCRIPT LANGUAGE="JavaScript">
The <SCRIPT> tag is the tag used to identify a block of script code to the browser. The LANGUAGE attribute defines the language of the script.
<!--Engage Cloaking Device
This line marks the beginning of a comment in html. Thus, everything between this line and the ending html comment line will be treated as a comment as far as the displaying of the document goes. This is NOT a comment marker in javascript however, so the script will be executed, but not displayed.
document.write("<CENTER><H2>Information</H2>");
The document.write command causes the string of characters defined between the parenthesis to be written into the document which is about to be displayed. Characters between the quote marks are displayed literally. Therefore, html tags can even be written into the document.
document.write("Code name: " + navigator.appCodeName + "<BR>");
Characters not between quote marks are names of variables or constants. In this case, navigator.appCodeName is a value which is passed to the script from the browser. The plus sign causes the strings of characters to be concatinated together.
document.write("Application name: " + navigator.appName + "<BR>");
document.write("Version: " + navigator.appVersion + "<BR>");
document.write("UserAgent: " + navigator.userAgent + "<BR><BR>");
document.write("According to the above information, ");
Again, the above 4 lines are writing more information to the document.
if (navigator.appName == "Netscape")
The IF statement tests a condition. In this case, it is testing to see if the name of the browser is Netscape. If it is, the next statement will be executed.
if (navigator.appVersion.lastIndexOf('Win') != -1)
This condition is testing to see if this is the Windows version of Netscape.
{
The curly braces enclose a block of statements which will be executed within a condition.
document.write("you are using the Windows version of Netscape.<BR>");
So, if the condition is true, write that information to the document.
}
And, close the block of code which is to be executed for that condition.
else
If the condition is false, then the ELSE statement is executed. If more than one IF statement has been executed before an ELSE statement is encountered (as in our example) then the first ELSE statement is associated with the last (most recent) IF statement, and so on.
{
document.write("you are using the Macintosh, Unix or another version of Netscape.<BR>");
}
Another block of code which writes the appropriate information in the case when the condition is false.
else
And, as described previously, this ELSE statement is associated with the first IF statement. That is, this block of code will define what to do if the browser is not Netscape.
{
document.write("you are using Microsoft Internet Explorer.<BR>");
}
This is the end of the conditional statements.
document.write("<P></CENTER>");
Write any final information to the document, in this case, start a new paragraph and close the CENTER tag.
//Disengage Cloaking Device -->
Here is the closing html comment marker. The double slash marks make the line a comment in the script language, so the script processor will not attempt to execute it.
</SCRIPT>
And, of course, we close the SCRIPT tag.