Event bindings and in-depth-multiple window.onload

Source: Internet
Author: User

Tag: Name exists multiple Eve classname red recursive ack obj

Event binding and drill-down with multiple window.onload execution

How to make the execution of two window.onload
Window.onload=function () {
Alert (' Lee ');
}

if (typeof window.onload== ' function ') {
var saved=null; Save the previous event object
Saved=window.onload;
}

Saved is window.onload,saved () equivalent to Window.onload (), but window.onload () cannot perform the
So saved () equals Window.onload=function () {}

Window.onload=function () {
if (saved) saved (); Perform the previous event
alert (Mr.Lee); Perform this event
}

----------------------------------------------------
Event switcher
<style>
. red{
width:100px;
height:100px;
background:red;
}
. blue{
width:100px;
height:100px;
Background:blue;
}
</style>

<div id= "box" class= "Red" > Test div</div>


Adding event functions
obj equals window
Type is equivalent to onload
fn equals function () {}
function Addevent (OBJ,TYPE,FN) {
Used to save the previous event
var saved=null;
Determine if an event exists
if (typeof obj[' on ' +type]== ' function ') {
saved=obj[' on ' +type]; Save Previous event
}
Execution events
obj[' on ' +type]=function () {
if (saved) saved ();
Fn.call (this); Pass this to the past
}
}

addevent (window, ' Load ', function () {
Alert (' Lee ');
})


addevent (window, ' Load ', function () {
Alert (' Mr.Lee ');
})


addevent (window, ' Load ', function () {
Alert (' Miss.lee ');
})

The following code event switches:
addevent (window, ' Load ', function () {
var Box=document.getelementbyid (' box ');
addevent (box, ' click ', function () {
Alert (' Lee ');
});
addevent (box, ' click ', Toblue);
})

function tored () {
This.classname= ' Red ';
Removeevent (This, ' click ');
Addevent (This, ' click ', Toblue);
}

function Toblue () {
This.classname= ' Blue ';
Removeevent (This, ' click ');
Addevent (This, ' click ', tored);
}
When constantly switching, the browser suddenly stuck, and error: Too much recursion, too many recursion
Because of the accumulation of too many saved events,
The solution is to use up the event and remove it immediately.

function Removeevent (obj,type) {
if (obj[' on ' +type]) obj[' on ' +type]=null;
}

Event bindings and in-depth-multiple window.onload

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.