JavaScript hook mechanism

Source: Internet
Author: User

The hook mechanism is like this, you write a method according to a rule (this rule is on the method name), and then the page is loaded before the unified execution of all the hooks function.

Note the Callhooks method, where the local variable s is the name of the hook function must have content. -This is the way to use hooks!

The object that handles the hook is var hook = (function () {return {timer:null, init:function () {This.callhoo          KS (' init ');               }, Callhooks:function (init) {var s = "Hook_" + init + ' _event ', f = [];                  for (Var h in window) {if (H.indexof (s)! = 0) Continue;              F.push (h);          } this.hookstimeout (f); }, Hookstimeout:function (hooks) {if (0 = = = hooks.length) {if (This.timer) cleartime                  Out (This.timer);              Return              } var h = Hooks.shift ();              Window[h].apply ();               WINDOW[H] = undefined;          Window.settimeout (This.hookstimeout (hooks), 200);   }         }     }());  Hook 1 var hook_init_event_tpl_html = function () {document.getElementById (' Test '). InnerHTML = ' This is html! '; }//Hook 2 var hook_init_event_tpl_console = function () {Console.Log (' This is console! ');  }//preferably before the page is finished loading, that is, before Window.onload () Hook.init ();

  

Summarize

Many problems, if we discuss together is very helpful, in the process we can learn a lot of things, we can also in the process from the cattle to school to many ways and thinking process, almost every technical seminar I have a lot of good harvest-I do not know if this is the legendary "brainstorming."

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.