
We cause a wind (scripts)
The main technics{technical equipment}, which we use to save ours Javascript unostentatious, this storage of a script in a separate file that is more preferable than to mix it with a marking of page. To execute functions in ours .js a file, we should call them when the page is loaded. It can be achieved by various ways, everyone has the merits and demerits.
Old school
Once for a long time, when we were young and innocent, we added attribute onload to an element body.
<body onload = " foo (); ">
That we should remember - we acted badly and more so we shall not be!
If we cause a script (y) in an element body, we come back to old as we continue to mix
Marking and calls of events. To move a call of a script to a file .js - that to us it is necessary to make.
Way with branch
We cause a script, attaching a call to event onload object window.
When at us only one function, we do not put parentheses on the end of a name of function, it should return results
It is better than function, than to start function.
If at us more than one function, we should to call the others, to use anonymous function, this time already with
Parentheses.
Javascript:
window.onload=foo;
Or
window.onload=function () {
foo ();
bar ();
baz ();
}
This method of a call of functions is applicable, certainly, not only to object window. As shown earlier, we can add it to any object of page. It is supported by everyone Javascript/DOM a browser.
Lack of this decision that we not absolutely nenavjazchivy. If we have more than one connected .js, caused scripts can copy calls onload one at another.
Rather unostentatious way.
There is an opportunity to add obrabotchiki events in the complete set to already existing obrabotchikam. It is rather sad, that various browsers are equipped with this functionality differently, and IE on Mac'akh so is not equipped at all.
This functionality is realized, for example, in the perfect repeatedly used function written Scott Andrew. By a call of function you pass her object which needs to be adhered to event, type of event and a name of function.
function addEvent (obj, evType, fn) {
if (obj.addEventListener) {
obj.addEventListener (evType, fn, false);
return true;
} else if (obj.attachEvent) {
var r = obj.attachEvent ("on" +evType, fn);
return r;
} else {
return false;
}
}
addEvent (window, ' load ', foo);
addEvent (window, ' load ', bar);
Obvious existing lack that function does not adhere anything in IE on Macintosh.
Voice of the Opera.
I have received the letter from service of technical support Opera explaining, that in the previous example there are omissions, therefore now he differs from an example on a site of Scott.
Function addEvent not quite corresponds{meets} to standards. She like should work in FireFox but since they pofiksili this bug, she for certain will not be more
To work in FireFox. Function it will not be possible to do{make} that that you want in the Opera which supports standards correctly.
Problem in that you adjust "intercepting" obrabotchik events, using "true" as the third argument in function addEventListener:
if (obj.addEventListener) {
obj.addEventListener (evType, fn, true);
This true should be false. If the variable is equal true, proslushka events should not be raised for one element for which she is registered. For example, if you use it with an element LI, she will not do{make} anything while LI has affiliated elements. Check up in the Opera
And more it is a little
Events - very multiplane and confusing subject in Javascript. It is enough for development of the websites, the specified examples, but if we pass to development of webs - applications, we can want the greater. Check up the links specified in addition to useful clauses{articles} to receive more information on events and on, how to adhere them to elements