JavaScript. Handling events

An event occurs when something happens in a browser window. The kinds of events that might occur are due to:

-A document loading; -The user clicking a mouse button;

-The browser screen changing size.

When a function is assigned to an event handler, that function is run when that event occurs. A handler that is assigned from a script used the syntax '[element].[event] = [function];', where [element] is a page element, [event] is the name of the selected event and [function] is the name of the function that occurs when the event takes place.

For example: document.onclick = clickHandler;

function clickHandler(evt) {

evt = evt || window.event; //some code here}

Standard event handlers

onabort Loading of image was interrupted

onblur Element loses focus

onchange Element gets modified

onclick Element gets clicked

ondblclick Element gets double clicked

onerror An error occurred loading an element

onfocus An element received focus

onkeydown A key was pressed when an element has focus

onkeypress A keystroke was received by the element

onkeyup A key was released when the element has focus

onload An element was loaded

onmousedown The mouse button was pressed on the element

onmousemove The mouse pointer moves while inside the element

JavaScript. User prompts.

The prompt dialog box is very useful when you want to pop-up a text box to get user input. Thus it enable you to interact with the user. The user needs to fill in the field and then click OK.

This dialog box is displayed using a method called prompt() which takes two parameters (i) A label which you want to display in the text box (ii) A default string to display in the text box.

This dialog box with two buttons: OK and Cancel. If the user clicks on OK button the window method prompt() will return entered value from the text box. If the user clicks on the Cancel button the window method prompt() returns null.

You can use prompt dialog box as follows:<head>

<script type="text/javascript">

<!--

var retVal = prompt("Enter your name: ", "your name here");

alert("You have entered: " + retVal);

//-->

</script>

</head>


Понравилась статья? Добавь ее в закладку (CTRL+D) и не забудь поделиться с друзьями:  



double arrow
Сейчас читают про: