JS Dynamic add onload, OnResize, Onscroll event (alternative method) _javascript skills

Source: Internet
Author: User
Window's onload, onresize, onscroll events, unlike other events, cannot be added with attachevent or AddEventListener.

That is to say, it can only do so (in the case of onload):
Copy Code code as follows:

Window.onload = function ()
{
// ...
};

But there is a problem, that is, if you want to add new event handlers for onload, you cannot directly assign values to the Window.onload, otherwise the previous assignment will be overwritten.

can do this
Copy Code code as follows:

var oldloadhandler = window.onload;
Window.onload = function ()
{
if (Oldloadhandler)
{
Oldloadhandler ();
}
Newloadhandler ();
};

In the EZJ, it is more convenient.
Copy Code code as follows:

$ (window). Ready (OnloadHandler1);
$ (window). Ready (ONLOADHANDLER2);


Description
We generally contact the onload event is document.body.onload, but this is actually due to IE misleading, the correct should be window.onload,window.onload in IE, Firefox, Chrome is effective.

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.