The next thing we are going to learn to do is a plug in detection. This script will be able to check if the user has a plug-in required to see your webpage correctly. Here is an example:
This script plays the movie file if the user has QuickTime or shows a picture if they do not. The <embed> tag is used to put QuickTime movies into HTML documents.
The next section describes how to make a loop. In this example we will learn how to detect
multiple plug-ins. Once again, here is an example:
<SCRIPT LANGUAGE=JAVASCRIPT
TYPE="TEXT/JAVASCRIPT">
<!-- Hide script from old browsers
flashDetect = false
for (i=0; i<navigator.plugins.length;i++) {
if (navigator.plugins[i].name.indexOf("Flash")>=0) {
flashDetect = true
}
}
// End hiding script from old browsers -->
</SCRIPT>
<BODY BGCOLOR=WHITE>
<H2>
<SCRIPT LANGUAGE=JAVASCRIPT
TYPE="TEXT/JAVASCRIPT">
<!-- Hide script from old browsers
if (flashDetect) {
document.write("You apparently have Flash!")
{
else {
document.write("You don't have Flash?")
}
// End hiding script from old browsers -->
</SCRIPT>
This example uses multiple codes in one HTML document to accomplish different tasks. First
this code sets up a boolean, which can only have a value of true or false. Next the for statement
begins the loop. It sets a counter(i) to zero and adds one to the counter until it becomes larger
than the number of installed plug-ins. The if statement checks the index and returns whether Flash
was found or not. If the test is true then the programs initializes the flashDetect write function
and if not it prints the else statement. Click HERE for a working example.
This next example covers calling a function. Functions are used to call information over and over.
There are always parenthesis after the function name and a set of braces. You will usually have an event
handler to call the function. Here is an example:
<SCRIPT LANGUAGE=JAVASCRIPT
TYPE="TEXT/JAVASCRIPT">
<!-- Hide script from old browsers
function callingText(message) {
alert(message)
}
// End hiding script from old browsers -->
</SCRIPT>
<BODY BGCOLOR=WHITE>
<H2>
<FORM>
<INPUT TYPE=BUTTON VALUE="Tyler" onClick="callingText('How much can you know about yourself if you have never been in a fight?')">
<INPUT TYPE=BUTTON VALUE="Matrix" onClick="callingText('Would you like the red pill or the blue one?')">
<INPUT TYPE=BUTTON VALUE="Insider" onClick="callingText('It does not matter whether you told the truth or not!')">
</FORM>
Here is an example:
In the function, the variable message is the parameter of the function. One good thing about parameters is that you could have a 1000 buttons and it would still use the same function.
The next thing that I'm going to show you is a concept known as scrolling status bars.
Here is an example:
<HEAD>
<TITLE></TITLE>
<SCRIPT LANGUAGE=JAVASCRIPT
TYPE="TEXT/JAVASCRIPT">
<!-- Hide script from old browsers
theMessage = "I'm learning Javascript!"
i = 0
function scrollMessage() {
window.status =
theMessage.substring(i,theMessage.length) +
theMessage.substring(0, i-1)
if (i < theMessage.length) {
i++
}
else{
i = 0
}
setTimeout(scrollMessage()",50)
}
// End hiding script from old browsers -->
</SCRIPT>
</HEAD>
<BODY BGCOLOR=WHITE onLoad="scrollMessage()">
After you create a function, a counter, and set the text variable, you transfer the info to the
status bar by creating a counter and setting the methods under window.status. The setTimeout tells
the browser when to stop running the message and the onLoad command tells the browser to load the
text as soon as the page is loaded. Click HERE for a working example.
**Note** This particular example will not work in the Netscape secondary window nor the AOL browser.
So far, my example only works on IE4 or higher.