Processing of events in language JavaScript
In javascript (or jscript, versions javascript firms microsoft) are two features: the first - that in him the functions are supported only, the second - that this language is clear for all browsers. The most habitual way of transfer of event to an element is an announcement of the name of function in tege an element. We shall notice, that we need to write down not simply "myclickcode", and " myclickcode () " for conformity of a syntactic design javascript. In html the document it will look so:
<h2 onclick = " myclickcode () ">
The text reacting to event
</h2>
...
<script language=javascript>
function myclickcode () {
alert (' you have clicked the Text! ');
}
</script>
It is good for job with separate elements and how to act{arrive} if necessary processings of events by the whole document? In this case indexes on obrabotchiki events can be placed in teg <body>:
<body onmousemove = " mymousemovecode () " onclick = " myclickcode ()>
Besides it is possible to connect a code of processing inside tega an element. Thus such design of language jscript is used:
<h2 language=jscript onclick = " alert (' you have clicked the Text! '); ">
The text reacting to event
</h2>
As jscript is the language of a browser determined by default, it is possible to lower{omit} attribute "language=jscript", having made a design of more compact:
<h2 onclick = " alert (' you have clicked the Text! '); ">
The text reacting to event
</h2>
And, at last, it is possible to create separate section <script> for each event which to us needs to be processed:
<h2 id=myheading> the Text reacting to event </h2>
...
<script language=javascript for=myheading event=onclick>
alert (' you have clicked the Text! ');
</script>
Processing of the events connected to windows, in language javascript.
We have considered how to place announcements of processing of events, such, as onmousemove in tege the document <body> for maintenance of reaction to the event which has occurred at a level of object document. Other situation - processing of event on a loss window. In internet explorer it is possible to place such instructions in teg opening <html>:
<html onmousemove = " mymousemovecode () "
onclick = " myclickcode () ">
...
</html>
There is one more reception of a designation of function processing event in line with the identifier of an element and event. In this case the name of event and a name of an element divide{share} a point. But thus it is necessary to mean, that these methods are not standard for processing events. Simply they work, because functions are set in properties of object element:
<h2 id=myheading> the Text reacting to event </h2>
...
<script language=javascript>
function myheading.onclick () {
alert (' you have clicked the Text! ');
}
</script>
This method use for processing the events connected to the basic objects of a browser - document and window:
<script language=javascript>
function window.onload () {
alert (' Loading of page is completed. ');
}
</script>
Cancellation of action of event.
Some events, such as onsubmit, allow to operate that as the browser will lead itself in reply to them, returning control value. We shall consider an example with the form containing a unique text field email, and the button submit (Sending):
<form id=myform onsubmit = " return checkaddress () "
action = " http: // www.some-web.com/file.cgi ">
<input type=text id=email>
<input type=submit>
</form>
<script language=javascript>
function checkaddress () {
straddress = document.forms ["myform"] .elements ["email"] .value;
if (straddress.indexof ()! =-1) // contains a symbol
return true
else
}
alert (' Check up correctness e-mail addresses! ');
return false
}
}
</script>
Here for search of a symbol in a line entered by the user in the form, function " indexof () " is used. If in line there is no such symbol, function will return value-1. In this case it is meant, what is it cannot be correct e-mail. About it the message is given out, and sending is cancelled by transfer of value false. We had been used a keyword return in attribute of an element onsubmit so the result comes back in that part of a browser which is engaged in sending.
In the resulted example it is visible, as it is used brouzernaja model for reception of the text from a text field. The required line is property value an element email. This form - a part of a collection elements forms myform, stored{kept} in a collection forms object document.
Instead of returning value actually function we can cancel the action by default appointed for the given event, with the help of property returnvalue object event. We shall consider it in the following releases.

|