Common methods for adding and removing JavaScript functions _javascript Tips

Source: Internet
Author: User
Ways to add a function
function addevent (node, type, listener) {
if (Node.addeventlistener) {
The method of the Consortium (DOM method)
The false meaning in the following statement is for the bubbling phase, and if true is for the capture phase (ie does not support capturing), the reason for this is false is to unify
Node.addeventlistener (type, listener, false);
return true;
else if (node.attachevent) {
Msie method (ie method)
Node[' E ' + type + listener] = listener;
Node[type + Listener] = function () {
Node[' E ' + type + listener] (window.event);
};
Node.attachevent (' on ' + type, Node[type + listener]);
return true;
}
return False if neither of the methods is available
return false;
}


Methods of removing functions
function removeevent (node, type, listener) {
if (Node.addeventlistener) {
Node.removeeventlistener (type, listener, false);
return true;
else if (node.detachevent) {
Node.detachevent (' on ' + type, listener);
return true;
}
return False if neither of the methods is available
return false;
}


The method to add a Load event is as follows
1.function Addloadevent (func) {
var oldonload=window.onload;
if (typeof window.onload!= "function") {
Window.onload=func;
}else{
Window.onload=function () {
Oldonload ();
Func ();
}
}
}
2.//using the Addevent method above
addevent (window, ' Load ', FN);

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.