Js dynamic addition of onload, onresize, onscroll events (Alternative method)

Source: Internet
Author: User

The onload, onresize, and onscroll events of windows are different from other events. They cannot be added using attachEvent or addEventListener.

That is to say, it can only be like this (take onload as an example, the same below ):
Copy codeThe Code is as follows:
Window. onload = function ()
{
//...
};

However, when you want to add a new event handler for onload, you cannot assign a value to window. onload directly. Otherwise, the previous assignment will be overwritten.

You can do this.:
Copy codeThe Code is as follows:
Var oldLoadHandler = window. onload;
Window. onload = function ()
{
If (oldLoadHandler)
{
OldLoadHandler ();
}
NewLoadHandler ();
};

In ezj, it is more convenient.
Copy codeThe Code is as follows:
$ (Window). ready (onloadHandler1 );
$ (Window). ready (onloadHandler2 );


Description
The onload event we normally contact is document. body. onload, but this is actually caused by incorrect IE. the correct one should be window. onload, window. onload is valid in IE, Firefox, and Chrome.

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.