A happy idea can be to give a welcome message according to the circumstances, for instance, a different message for every hours of the day, and so on.
This script is compatible with Explorer, Netscape 6, Mozilla, Safari, Opera.
You must put this script inside of
body and
/body tags in your web page, where you want to dispay the message.
script language="JavaScript" type="text/JavaScript"
oracorrente = new Date
if (oracorrente.getHours() < 5) {
document.write("What are you doing here in the middle of the night??")
}
else if (oracorrente.getHours() < 12) {
document.write("Good morning!!")
}
else if (oracorrente.getHours() < 17) {
document.write("Good afternoon!!")
}
else {
document.write("Good evening!!")
}
/script
Note:
In the example above, we have deliberately removed less and major sign before and after the opening tag SCRIPT and the closing tag /SCRIPT. To use correctly the script you have to put them in.
Here is the url to the script in .txt file
Analysis of the script
The
oracorrente variable takes from the
new Date object the visitor's PC local time.
Now, by means of the
getHours() method, you take from the
oracorrente variable the current time (
oracorrente.getHours()).
Then, by means of some conditional expression (if/else if/else), comparing the local time (oracorrente.getHours()) with the conditions, you will get a message ad hoc.
For example in the first case: if the hour value is less than 5, the displayed message will be: What are you doing here in the middle of the night??.
And so on for the other conditions.
You can also customize the output messages as you like.
You remember with
document.write() method, you can write whole HTML pages (not only little messages).
Furthermore, you can replace the message text
What are you doing here in the middle of the night?? with HTML code.