JavaScript Tip of the Week for June 24, 1996: Too Many Browers? Wrong. | Source Code
for June 24, 1996: Source Code: Too Many Browers? Wrong.
Jump to source of:
Detect which browser is being used, should be put in head area of page:
/* This code is Copyright (c) 1996 Nick Heinle and Athenia Associates,
* all rights reserved. In order to receive the right to license this
* code for use on your site the original code must be copied from the
* Web site webreference.com/javascript/. License is granted to user to
* reuse this code on their own Web site if and only if this entire copyright
* notice is included. Code written by Nick Heinle of webreference.com.
*/
var version = 0;
if (navigator.userAgent.indexOf("Mozilla/3.0") != -1) version = 3;
else if (navigator.userAgent.indexOf("MSIE") != -1) version = 1;
else if (navigator.userAgent.indexOf("Mozilla/2.0") != -1) version = 2;
else version = 0;
Quick function to display which browser is being used:
function which_one()
{
if (version == 3) return "Netscape Version 3.0";
if (version == 2) return "Netscape Version 2.x.";
if (version == 1) return "Microsoft Internet Explorer 3.0";
if (version == 0) return "A browser I've never heard of"
}
... which is activated by this image button:
<A HREF = "JavaScript:alert('You are using ' + which_one())">
<IMG HEIGHT = 42 WIDTH = 105 BORDER = 0 SRC = "test_inactive.jpg"></A>
Detect which browser is being used and activate image commands if Netscape 3.0 is detected:
var version = 0;
if (navigator.userAgent.indexOf("Mozilla/3.0") != -1)
{
version = 3;
active = new Image(105, 42);
active.src = "test_active.jpg";
inactive = new Image(105, 42);
inactive.src = "test_inactive.jpg";
}
else if (navigator.userAgent.indexOf("MSIE") != -1) version = 1;
else if (navigator.userAgent.indexOf("Mozilla/2.0") != -1) version = 2;
else version = 0;
If Netscape 3.0 detected, print out grey image button that changes, otherwise print out normal image button:
Note: each document.write should be in one continuous line.
if (version == 3)
{
document.write("<A HREF = \"JavaScript:alert(\'You are using \' + which_one\(\)\)\"
onMouseOver = \"change_button\(\);return true\"><IMG HEIGHT = 42
WIDTH = 105 BORDER = 0 SRC = \"test_inactive.jpg\"></A><BR>");
}
else
{
document.write("<A HREF = \"JavaScript:alert(\'You are using \' + which_one\(\)\)\">
<IMG HEIGHT = 42 WIDTH = 105 BORDER = 0 SRC = \"test_active.jpg\"></A>");
}
Run this function to change the grey image button when mouse moves over it, put 0 in for x if image is first in document, 1 in for x if second image in document, etc.:
function change_button() {
document.images[x].src = active.src;
setTimeout( "document.images[x].src = inactive.src", 2000);
}

Find a programming school near you