SoFunction
Updated on 2025-02-28

js load multiple functions and append functions in detail

I often need to use it for projects.

The usage is as follows:

function func(){alert("this is window onload event!");return;}

=func;

Or as follows:

=function(){alert("this is window onload event!");return;}

However, multiple functions cannot be loaded at the same time.

for example:

function t(){
alert("t")
}
function b(){
alert("b")
}
=t ;
=b ;

The previous one will be overwritten later, and the above code will only output b.

At this time, the following methods can be used to solve the problem:
=function() { t();  b(); }

Another solution is as follows:

Copy the codeThe code is as follows:

 function addLoadEvent(func) {
var oldonload = ;//Get the function of the previous onload event
if (typeof != 'function') {//Judge whether the type is 'function', note that typeof returns a string
    = func;
  } else { 
    = function() {
oldonload();//Call the function of the onload event that was covered before----> Since I don’t know much about js, here I temporarily understand that it is to load multiple functions by overriding the onload event function
func();//Calling the current event function
    }
  }
}

//(full example) is used as follows:

function t(){
alert("t")
}
function b(){
alert("b")
}
function c(){
alert("c")
}
 function addLoadEvent(func) {
  var oldonload = ;
  if (typeof != 'function') {
    = func;
  } else { 
    = function() {
      oldonload();
      func();
    }
  }
}

addLoadEvent(t);
addLoadEvent(b);
addLoadEvent(c);
//Equivalent to =function() { t();  b(); c() ;}


I personally think it is faster to use implicit functions directly (such as: =function() { t();  b(); c() ;}). Of course, use addLoadEvent to be more professional, and everyone can do it!

JS append function:

Copy the codeThe code is as follows:

<script>
if()//IE: If a function exists in the browser, use the function. You can also use it to determine whether it is IE: if (){//..}
("onload",function() {alert("add method");});
else  //FireFox
("load",function() {alert("add method");},true);
</script>

Run, alert pops up messages in js, and the problem is solved.

==================================

attachEvent   Binds the specified function to an event so that the function is called every time the event is fired on the object.

Internet Explorer has provided an attachEvent method since 5.0. Using this method, you can assign multiple processing procedures to an event. attachEvent also applies to current Opera. But Mozilla/Firefox does not support this method. However, it supports another addEventListener method, which is similar to attachEvent and is also used to assign multiple processing procedures to an event. However, there are some differences in the events they assign. In the attachEvent method, the event starts with "on", while in the addEventListener, the event does not have the "on" that begins. In addition, the addEventListener has a third parameter. Generally, this parameter is specified as false.