JS window.onload Load multiple functions and append functions

Source: Internet
Author: User

  This article is mainly on the JS window.onload loaded with multiple functions and append functions are introduced, the need for friends can come to the reference, I hope to help you.

Usually do projects often need to use window.onload,   usage is as follows:   function func () {alert ("This is window onload event!"); return;}     Window.onload=func;   or below:    window.onload=function () {alert ("This is window onload event!"); return;}   But window.onload cannot load multiple functions at the same time.   For example:   function t () {alert ("T")} function B () {alert ("B")} window.onload =t; Window.onload =b;   will be preceded by the overlay, the above code will only output B.     This can be resolved at this point: Window.onload =function () {t ();  b ();}   Another workaround is as follows: The   code is as follows:  function Addl Oadevent (func) {  var oldonload = window.onload;//Gets the last onload event's function   if (typeof window.onload ' function ') {/ /Determine whether the type is ' function ', note that TypeOf returns a string     window.onload = func;  } else {      window.onload = function () {      oldonload ();/functions to invoke the OnLoad event previously overridden--- -> because I do not know much about JS, here I temporarily understand that by overwriting the OnLoad event function to achieve load multiple functions       func ();//Call Current event function    }  }   (complete example) is used as follows:   FUNCTIOn t () {alert ("T")} function B () {alert ("B")} function C () {alert ("C")}  function Addloadevent (func) {  var Oldonload = window.onload;   if (typeof window.onload!= ' function ') {    window.onload = func;  } else {      Window.onload = function () {      oldonload ()       func (),    }  } &NB Sp Addloadevent (t); Addloadevent (b); Addloadevent (c); Equivalent to  window.onload =function () {t ();  b (); C ();}     personally think of using implicit functions directly (such as: Window.onload =function () {T ( );  b (); C ();}) Faster, of course, use addloadevent more professional, each take the good!   JS window.onload append function:     Code as follows: <script> if (window.attachevent)//ie: If there is a window.attachevent function in the browser, use the Window.attachevent function to determine if IE can also use: if (document.all) {//.} window.attachevent (" OnLoad ", function () {alert (" Add Method ");}); else  //firefox window.addeventlistener ("Load", function () {alert ("Add Method");},true); </script>   Running, JS in aLert pop-up message, problem resolution.   ============ Related Materials ================   attachevent   Bind the specified function to an event so that it is called whenever the event fires on an object.   Internet Explorer provides a attachevent method starting with 5.0, which allows you to assign multiple processes to an event. Attachevent is also suitable for the current Opera. But Mozilla/firefox does not support this approach. But it supports another AddEventListener method, which is similar to attachevent and is used to assign multiple processes to an event. But the events they assign are somewhat different, in the Attachevent method, the event starts with "on," and in AddEventListener, the event has no "on" at the beginning, and the other AddEventListener has a third parameter, which is typically specified as False is OK.  

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.