function | What if you have a "OnLoad" event in your Web page that activates more than two JavaScript functions? Sunny little has consulted a lot of data, the results are not satisfactory, nothing but the programmer according to the situation, the function to be activated unified in a function to activate.
But as a programmer, the applicability of code written is important. Take a look at one of the following examples:
<body onload= ' alert ("This is the function of the <BODY> tag call") ' >
<script language= "JavaScript" >
function MyFunction () {
Alert ("This is the function called by the JS script");
}
Document.body.onload=myfunction;
</script>
</BODY>
After running, you will find that it only jumps out a warning box of "This is a function called by the JS script." The function required to activate in the < body> tag is "ignored". Of course, you can use the method mentioned earlier to activate two functions in one function. But there are a lot of drawbacks, especially if you write this JavaScript code as a "myjs.js" file, use it directly with the words < script language= "javascript" src= "Myjs.js" ></ Script> can be, but if you want to refer to the file on the page to modify the label or add a piece of code, much trouble. This reduces the applicability of the code.
Can you design JavaScript code, if the front onload event already has a function to activate, so that it still activates the previous function, but also activates the function required by this code? The answer is yes. Let's take a look at this piece of code first:
<body onload= ' alert ("This is the function of the <BODY> tag call") ' >
<script language= "JavaScript" >
Document.body.onload is a function type, and the ToString () function is used to convert to string (string) type:
Alert (document.body.onload.toString ());
</script>
</BODY>
After running one of the warning boxes prompts:
function Anonymous ()
{
Alert ("This is the tag called function")
}
Have you found anything yet? "Document.body.onload.toString ()" is the above function code! Our string is running as part of the code, so we have one more function "anonymous ()", so we activate the function together with the function we need to activate now!
Here's what we'll do with the code we mentioned for the first time, and get the final code:
<body onload= ' alert ("This is the function of the <BODY> tag call") ' >
<script language= "JavaScript" >
function MyFunction () {
Alert ("This is the function called by the JS script");
}
file://is now in the function we want to activate is "myfunction ()", the function to be activated is "alert ()" In the <BODY> tag,
file:// This code can be activated by two together:
Myactive= "MyFunction ()";
if (document.body.onload.toString ())
{
Eval (document.body.onload.toString (). Replace (' Anonymous () ', ' bodyload () '));
Document.body.onload=new Function ("Bodyload ();" +myactive);
}
else document.body.onload=new Function (myactive);
</script>
</BODY>
In the previous code, the variable myactive is the function to activate in the JS code, and the eval (string) function is to run its string argument as a JavaScript script. We replace "Anonymous ()" in document.body.onload.toString () with "bodyload ()" To get a new function "Bodyload ()", which contains all the functions that were originally activated, and then the function " Bodyload () is activated in the OnLoad event along with the function we want to activate.
Why replace "anonymous ()" with "Bodyload ()"? Because this method applies to all events (such as onmouseover), and all Events ToString () is "anonymous" (), we replace it in order not to use this method of conflict with other events. This is also replaced in other events.
All right, let's run the final code, and as you wish, two functions are activated? Added code is also quite simple, has a very strong applicability.