Practical JavaScript Programming: Simple JavaScript Scripts
JavaScript Simple Scripts
Practical JavaScript Programming
by Reaz Hoque ([email protected])
Since Microsoft introduced Internet Explorer 3.0, JavaScript has gained a quite a bit of recognition. The reason being? Well, now JavaScript is supported by the most popular browsers on the web: Netscape Navigator 2.0+ and Microsoft Explorer 3.0 (aka IE3.0). If you have some knowledge on this easy to learn but powerful scripting language, I would like to encourage you to try some of the simple JavaScript snippets from this article.
In the article, I will show you some neat little scripts that you can use in your homepages and really surprise others who are not familiar with this language. Some of the examples presented here will be useful as well. For example, Now that we have different versions of the Netscape browser and only the latest can handle certain plug-ins or JavaScript syntax, you might want to know which version of the browser your visitor is using. If your visitor is using Navigator 2.0 and you have JavaScript 1.1 on your page, the client's browser surely will crash. Detecting a browser is a great example for making sure that you can offer your page to all of your clients, not just the ones who have the latest browser. Another example would be displaying the latest update date of your Web page. If your visitor knows when your page was updated, they can choose whether or not to surf the page. Who likes to browse through month-old information? The same concept will apply to some of the other scripts that are presented here. Just by cutting and pasting some of this code, you can make your site a better looking and more enjoyable place to visit. Your first JavaScript script Writing text on a document <HTML> <HEAD> <TITLE> Writing text on a document</TITLE> </HEAD> <BODY> <SCRIPT LANGUAGE="JAVASCRIPT"> <!-- Hiding the code document.write("Welcome to Applications in JavaScript!"); // done hiding --> </SCRIPT> </BODY> </HTML>
If you're familiar with JavaScript
syntax, this script should be easy for you to understand. You will
notice that we have included the code in between
Now, you might be wondering what the difference is between
Date and time If you look at the Netscape's JavaScript documentation, you will know that there are two built-in functions in JavaScript that let you display date and time. The following example will display the current time and date in a text box with an option to stop the time. Showing date and time on a document <HTML> <HEAD> <TITLE> Showing date and time on a document</TITLE> <SCRIPT LANGUAGE="JAVASCRIPT"> <!--Hiding the code var show_time=false; var timerID=null; function stop(){ if (show_time){ clearTimeout(timerID); document.clock.date_time.value=" "; } show_time=false; } function start(form){ var today=new Date(); var display_value =" Time= " + today.getHours() if(today.getMinutes() < 10){ display_value+=":0" + today.getMinutes(); } else{ display_value+=":" + today.getMinutes(); } if (today.getSeconds() < 10){ display_value+=":0" + today.getSeconds(); } else{ display_value+=":" + today.getSeconds(); } if(today.getHours()>=12){ display_value+=" P.M." /* Here we have a variable called mygreeting where you can store somthing like this: mygreeting ="Good Morning!"; */ } else{ display_value+=" A.M." /* Now set mygreeting to: mygreeting ="Good Afternoon!"; */ } display_value += " Date= " + (today.getMonth()+1) + "/" + today.getDate() + "/" + today.getYear(); document.clock.date_time.value=display_value; timerID=setTimeout("start()",100); show_time=true; /* Here have an alert() method do the following: alert(mygreeting); */ } //done hiding--> </SCRIPT> </HEAD> <BODY BGCOLOR=white Text=Red Link=Green onLoad=stop()> <center> <H2>Displaying Date and Time</H2> <FORM name=clock> <INPUT type="text" name="date_time" size=35 value=" "><br> <INPUT type="button" name="show_now" value="Display" onClick=start()> <INPUT type="button" name="clear_now" value=" Clear " onClick=stop()> </center> </FORM> </BODY> </HTML>
In this script, we have two functions (
Now the
In
In order to display a.m. or p.m., another document.clock.date_time.value=display_value;The above code takes the value of display_value and puts
it in the property called value of the text box named
date_time . The word document is used to insure that we
are indicating the current document. Lastly, the variable
timerID is set and show_time is set to true.
Notice that timeID uses the setTimeOut()
function with the parameters start() and 100. The reason
why 100 is passed in is because the function start() is
within one tenth of the second.
Displaying automatic page
update information Displaying automatic page update information <HTML> <HEAD></HEAD> <TITLE> Displaying Update Info</TITLE> <BODY bgcolor=ffffff> <script language="JavaScript"> <!--hide script from old browsers document.write("<h2>This page has been updated: " + document.lastModified + "</h2>"); // end hiding --> </script> </BODY> </HTML>
All you needed to do here is use the
Measuring users' time
on a page Measuring a user's time on a page <HTML> <HEAD> <TITLE>Detecting User's Time on a Page</TITLE> <SCRIPT LANGUAGE="JavaScript"> function person_in() { enter=new Date(); } function person_out() { exit=new Date(); time_dif=(exit.getTime()-enter.getTime())/1000; time_dif=Math.round(time_dif); alert ("You've only been here for: " + time_dif + " seconds!!") } </SCRIPT> </HEAD> <BODY bgcolor=ffffff onLoad='person_in()' onUnLoad='person_out()'> </BODY> </HTML>
Detecting a particular 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> 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
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
This script can also alert visitors that if they want to view this page, they need to acquire the appropriate browser.
Warning: The
Playing 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="ex5_:play()"><img src="sound.jpg" border=0></a> </body> </HTML>
First we had an image that calls the function 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").
Scrolling Banner The difference between this scrolling banner and others you have seen is that you can control the banner's speed and pause the scrolling. Let's see the script: Scrolling banner <HTML> <HEAD> <SCRIPT LANGUAGE="JavaScript"> var b_speed=8; //defines banner speed var banner_id=10; var b_pause=0; //to pause the banner var b_pos=0; function stop() { if(!b_pause) { clearTimeout(banner_id); b_pause=1; } else { banner_main(); b_pause=0; } } function banner_main() { msg="W e l c o m e to J a v a S c r i pt!" +" JavaScript can do some really" +" Cool stuff. Check out https://rhoque.com" +" for more examples..." var k=(40/msg.length)+1; for(var j=0;j |
Comments are welcome
Copyright © 1996 Reaz Hoque and
Created: November 24, 1996
Revised: November 25, 1996
URL: https://webreference.com/content/jspro/chap5/