Javascript Events

Events are actions that the user performs while visiting your site. Submitting a form or moving the mouse over an image are two examples for events.

JavaScript applies commands called Event Handlers to these events. An action of a user is recognized by an Event Handler in your script. For example, if the user clicks the button to abort loading your page, the Event Handler onAbort() will take a notice of it and perform whatever action you have assigned.

When you write a script you don’t have to define scenarios for every possible action there is. An image button will display just fine without using onMouseOver and onMouseOut. But if you want to add a little more excitement to your page’s interface, you should try it.

Event Handlers

EVENT

onAbort

onBlur

onChange

onClick

onError

onFocus

onLoad

onMouseOver

onMouseOut

onSelect

onSubmit

onUnload


WHAT IT HANDLES

User aborted page loading.

User left the object.

User changed the object.

User clicked on an object.

Script encountered an error.

User made an object active.

Object finished page loading.

Cursor moved over an object.

Cursor moved off an object.

User selected content of a page.

User submitted a form.

User left window.

Javascript Comparisons

Often you want to compare a value of one variable with one of another variable, or the value of a variable against a literal value, for example the value in an if-statement.

Example for a comparison is if you want to compare the value of the day of week to “Sunday”, you can do this by checking todaysDate == “Thursday”.

Don’t mix up ‘=’ with ‘==’, the first is an assignment, the second one is a comparison.

Comparisons

COMPARISON
x == y

x != y

x > y

x >= y

x < y

x <= y

x && y

x || y

!x


WHAT IT DOES
True if x and y are equal

True if x and y are not equal

True if x is greater than y

True if x is greater or equal to y

True if x is less than y

True if x is less or equal to y

True if x and y are true

True if x or y are true

True if x is false

Javascript Assignments

When you try to put a value in a variable you try to assign the value to the variable. To assign it, you have to use an assignment operator to get the job done. For example, use the equals to create an assignment, such as siteName = “HTMLcenter”.

Besides the equals sign, the other assignment operators serve as shortcuts for modifying the value of a variable. For example the shortcut for x = x + 5 is to use x += 5. If you don’t like the short way, just use the long version, it will not change the outcome, just a little more to type.

Assignments

ASSIGNMENT

x = y

x += y

x -= y

x *= y

x /= y

x %= y


WHAT IT DOES

Sets x to the value of y

x = x + y

x = x – y

x = x * y

x = z / y

x = x % y

Javascript Alert

Alert boxes are used to provide the user with important information. When users enter your site and you want to make sure that they sign your guestbook, you can use an alert box to remind them. Keep in mind, that overusing this function can get very annoying and your visitors might decide not to come back.

Alert Box Example:

<html>
<head>
<title>Alert Boxes</title>
<script>
<!-- Hide Script
	alert("My Own JavaScript Alert Box!")
//End Hide Script-->
</script>
</head>
<body>
<noscript>
  This page uses JavaScript.
  Please enable it or upgrade your browser.
</noscript>
</body>
</html>

alert(“My Own JavaScript Alert Box!”), that’s the code for your alert box. Put any text inside the quotation marks in alert() and it will pop up next time you load the page.

The JavaScript alert box will always say “JavaScript Application” and there’s no way to code around it. We suppose it’s a security feature so that a site owner can’t trick visitors into something using an alert box.

You might have seen that we used the <noscript> tag above. That’s because in non-JavaScript browsers (JavaScript turned off or not supported) a message will show up saying “This page uses JavaScript. Please enable it or upgrade your browser.”.

Hello World!

“Hello World!” is generally the first program you learn to write when you start with a programming or scripting language. Are you are new to JavaScript? In this tutorial you will learn how to use the document.write function to place text on a page using JavaScript. This tutorial will also show you, how to hide your scripts from older browsers and how to place comments in your scripts.

Hello World Example:

<html><head><title>Hello World</title></head>

<body>

<script>

 document.write("Hello, World!")

</script>

</body>

</html>

Our script is in between the <script> tags; <script> tells the browser to expect our JavaScript, </script> tells the browser to expect HTML again. You can have as many scripts as you want in your source code, this also means you can therefore run multiple scripts on the same page.

document.write(“Hello, World!”) is your JavaScript. It takes the document window and writes “Hello, World!” into it.

Of course your script can not be interpreted by old browsers, such as Netscape 1.x, Internet Explorer 3.x and earlier, and the America Online browser before version 4. While well working browsers are told to ignore what they do not “understand”, we use a technique to make sure.

Script Listening 1.2:

<html><head><title>Hello World</title></head>

<body>

<script><!-- Hide Script

 document.write("Hello, World!")

//End Hide Script-->

</script>

</body>

</html>

This will look like a comment to older browser and they will simply ignore it and not produce an error. In case you want to display a message to your site?s user if his browser doesn?t support JavaScript, add your message into the <noscript> tag.

The more advanced your scripts get, the more it becomes necessary for you to write comments inside your scripts. For example we could forget what a certain statement means and in order not to need to look it up in a book or here at HTMLcenter, you can write a comment next to it which explains what it does. If you distribute your scripts on your website, you could use comments to write a short notice, that tells who it was written by and where to find it, and other scripts.

Script Listening 1.3:

<html><head><title>Hello World</title></head>

<body>

<script>/* Script name: Hello, World!

   HTMLcenter - JavaScript Tutorial

   Date: 03-07-1999

*/

<!-- Hide Script

 document.write("Hello, World!")

// document.write prints text on the screen

//End Hide Script-->

</script>

</body>

</html>

There are two different ways to write comments. You can use “/*” and “*/” if the comments are more than one single line, instead of writing “//” at the beginning of each. We use “//” if your comments fit on one single line.

This is the end of our “Hello World” tutorial. You learned how to print a message on the screen, how to hide your scripts from older browsers or browsers that have JavaScript turned off, and you learned how to write comments inside your JavaScript. We suggest that you check out our next JavaScript tutorial and keep on scripting!

Please note, that if any of the HTML tags, you have read while going over this tutorial confuse you, or you are not quite sure about their meaning, please check out our HTML tutorials (Basics) at HTMLcenter.

Pages: