Simple JavaScript Scripts, Part 4
Detecting a particular browser
Our next example shows you how to detect a particular browser.
As mentioned earlier, this is useful because you could have a
page that supports JavaScript for only Netscape 3.0, therefore,
you don't want a visitor to visit the page without that browser.
Detecting the appropriate browser
<HTML>
<TITLE>Detecting User's Browser</TITLE>
<HEAD></HEAD>
<BODY BGCOLOR=ffffff>
<SCRIPT Language="JavaScript">
if (navigator.appName == "Netscape"){
if (navigator.appVersion.substring(0, 3) == "3.0"){
if (navigator.appVersion.substring(3, 4) == "b"){
alert('You are using :' + navigator.appName + ' (' +
navigator.appCodeName + ') ' + navigator.appVersion +
'\nSorry! You are not using Netscape 3.0+');
history.back();
}
}
}
else {
alert('Sorry! You are not using Netscape 3.0+');
}
</SCRIPT>
</BODY>
</HTML>
Test This Example
Here we use the some of the properties of the Navigator object. First we
find out if the browser is a Netscape browser. If so, we detect if the
version is 3.0. If the version is a beta version, we display the whole
browser information with its platform, and alert the user that he or she is
not using a Netscape 3.0 browser.
Notice that before we closed the if statement, we used
the history.back()
statement. It is used so that when the user presses OK on the alert message
box, the document automatically takes the user to the previous page. This is
useful because sometimes if you run JavaScript 1.1 on Netscape browser 2.0
or earlier, the browser might crash; this will prevent users from crashing
their browsers.
Here's another useful tip: You could also send the user to a different
page if the browser is not Version 3.0. Instead of the
history.back() statement, you need to type the following
statement: window.location="myotherpage.html".
This script can also alert visitors that if they want to view this page,
they need to acquire the appropriate browser.
Warning: The else statement is not effective unless you
use a JavaScript-enabled browser besides Netscape, such as Microsoft's
Explorer 3.0.
Playing on-demand sound
A page with sound can be really nice! This not only gives users
something to listen to when they are visiting, it also makes your site more
multimedia savvy. With JavaScript, you can play sound when the document
is loaded or exited, or when the user pushes a link. The following listing
will show you how we can use an image as a link for playing an on-demand sound.
Playing on-demand sound
<HTML>
<HEAD>
<TITLE>Playing on-demand sound</TITLE>
<SCRIPT LANGUAGE="JavaScript">
function play(){
window.location = "sample.au"
}
</SCRIPT>
</HEAD>
<body bgcolor=ffffff>
<h2>Playing on-demand sound:</h2>
<b>Please click on the image below</b><br>
<a href="http://www.rhoque.com/book/ex5_:play()">
<img src="sound.jpg" border=0></a>
</body>
</HTML>
Test This Example
First we had an image that calls the function play().
Notice the way we linked the function: javascript:play().
This makes sure that this hyperlink is a JavaScript link that should
call the function play(). The play function uses the
location property of the document object and simply points to the sound
file.
You should note that if you want to play other files such as a
Shockwave file, all you need to do is replace the "sample.au" with a
shock wave file (e.g."sample.dcr").
Simple JavaScript Scripts, Part 3
Simple JavaScript Scripts
Simple JavaScript Scripts, Part 5
|