DHTML Lab 10
Detecting and Controlling the Client's Environment


Detecting the browser type, version, and operating system
Since there are different browsers and many different versions of those browsers and since the browser makers have chosen to implement JavaScript and DHTML differently and because browsers appear differently on different operating systems it is sometimes important to be able to detect the browser, version and operating system and respond accordingly. Also, we can detect the client's screen size, broswer window size, whether or not they have javascript and/or java enabled, which plugins they have installed etc.

In DHTML lesson 8 we learned how to use JavaScript with Cascading Style Sheets in Netscape. In DHTML lesson 9 we learned how to use JavaScript with Cascading Style Sheets in Internet Explorer. They were different, but when you make webpages you want to make them for as many people to be able to see as possible, Therefore we need to be able to detect the clients browser and change the code dynamically in order that they see the wonderful job we have done!

The first and most important thing we need to be able to do is detect which browser and version the client is using.

Type the follow code into CodeRunner below:
<script language=javascript>
<!--
alert(navigator.userAgent);

//-->
</script>


Preview this example. It should pop up and tell you information about your browser.

Here are some examples of what you will see with different browsers and operating systems:

document.userAgentwhat it means
Mozilla/4.05 (Macintosh; I; PPC, Nav) Netscape 4.05 Navigator on a Mac
Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt) Internet Explorer 5.0 on Windows NT
Mozilla/4.51 [en] (Win98; I) Netscape 4.51 on Windows 98
Mozilla/4.51 [en] (X11; I; Linux 2.2.9 i586) Netscape 4.51 on Linux X-Windows


You see, Netscape puts the actual version number just after the "Mozilla/" whereas Microsoft puts the version after the MSIE. Both browsers, say Mozilla because they both use code from the original browser Mosaic created at NCSA in the early 90's. We can also use navigator.appName which returns only the name of the user's browser and none of theversion or operating system information. Try using navigator.appName instead of navigator.userAgent and see that it returns Netscape for netscape users and Microsoft Internet Explorer for Internet Explorer users.

Also, if we use Navigator.appVersion then it returns the same stuff as Navigator.userAgent only without the word Mozilla.

Now suppose you want to check for people using Netscape 4.0 or higher and respond somehow (later we'll do this to adapt our code to make things work in a cross browser way). Try the following:

Type the follow code into CodeRunner below:
<script language=javascript>


thebrowser = navigator.appName;
theversion = navigator.userAgent;

if (thebrowser == "Netscape" && (theversion.lastIndexOf("4.") >= -1 || theversion.lastIndexOf("5.") >= -1 )){

document.write("Your browser is Netscape, and is version 4.0 or higher.");
}
else if(thebrowser == "Microsoft Internet Explorer" && (theversion.lastIndexOf("4.") >= -1 || theversion.lastIndexOf("5.") >= -1)){

document.write("Your browser is Internet Explorer, and is version 4.0 or higher.");

}
else{

document.write("Your browser is not compatable with this site");

}

</script>


Preveiw this (make sure that if you copy and pasted that there are no return characters where they shouldn't be).

That's pretty much how we detect the browser type and version of the user and check for it. In Later tutorials we will be using this method to make our DHTML code and javascript work on both Netscape and Internet Explorer.

Detecting window and screen size

We can also detect the screen size or resolution on the computer the user is using, and we can detect the window size of the browser the user is using. These might be useful if you want to maximize the size of a pop-up window or you are animating something that moves across the browser window and need to know how far it can go. Let's go ahead and try both:

Type the follow code into CodeRunner below:
<script language=javascript>


if(navigator.appName == "Netscape"){

windowwidth = window.innerWidth;
windowheight = window.innerHeight;

}else if(navigator.appName == "Microsoft Internet Explorer"){

windowwidth = document.body.clientWidth;
windowheight = document.body.clientHeight;

}else{

windowwidth = "can't tell you";

}

document.open();
document.write("<br>screen width = " + screen.availWidth);
document.write("<br>screen height = " + screen.availHeight);
document.write("<br>this window width = " + windowwidth);
document.write("<br>this window height = " + windowheight);
document.close();

</script>


Preview this and make sure it works for you.

All of the values are in pixels. The screen.availWidth and screen.availHeight are the measurements of your monitors current resolution. Typical values are 640 width by 480 height, or 800 width by 600 height.

Notice that we had to check for the browser type before we checked for the window width. This is because Netscape and Internet explorer have two different ways of accessing the width and height of the window. In Internet Explorer we use document.body.clientWidth and document.body.clientHeight but in Netscape we use window.innerWidth and window.innerHeight

Both Netscape and Internet Explorer use screen.availWidth and screen.availHeight to get the height and width of your screen, by some strange accident.

Now you may be wondering for what purposes you might want to know the screen sizes and window sizes. Well, you may want to open a new window and set the size as a percentage of the screen size. Screen sizes vary radically from system to system, from 640 by 480 up to 1600 by 1200. If you open a screen to fit in one quarter of the screen for a 640 by 480 then it will be way to small for people using 1600 by 1200!

An frequent use for the knowing browser window size will come up when we animiate things on the web page later on, so I'll show you an example of opening a window as a percentage of the screen size.

Type the follow code into CodeRunner below:
<script language=javascript>


function openwindow(){

screen_width = .5*screen.availWidth;
screen_height = .5*screen.availHeight;

newwindow = eval("window.open('','','width=" + parseInt(screen_width) + ",height=" + parseInt(screen_height) + "')");
}
</script>

<body bgcolor=white>
<form>

<input type=button value="open a window" onclick="openwindow();">
</form>

</body>


Preview this, and click the button to see if it works for you. Then, see if you can figure out what's going on. Notice we used eval() so that we could get the variables in where a string is expected? It is a very common pratice to use eval this way. We also used parseInt because we can only use integers for the numbers for the width and height properties, but it is likely that half of the screen size will return numbers with decimal points. TRY CHANGING THE .5 TO SOME OTHER NUMBERS LESS THAT 1 AND SEE WHAT HAPPENS.

This is enough for now. We will have plenty of oppurtunities to use these methods in later DHTML lessons...See you there!