Perfect solution an event activates multiple functions (2)

Source: Internet
Author: User
Tags anonymous eval functions tostring
function | solution But in a page, we might encounter multiple events to activate multiple functions, and each event would have to write that code again, which is clearly not a good idea. Smart readers may have thought that it should be written as a function Activemore (), which event to activate multiple functions, directly using Activemore () on the line.

But in the last section we use the ToString () of the event as JavaScript to run, actually run a function code, if you write a function or use this method, that is nested functions in the function, there will be unexpected errors. So when we write the function, we remove the "function anonymous ()" character from the event's ToString (), and the runtime is not a function, but the code inside the function. Alternatively, we can define whether to activate the existing function first or activate the function you want to activate now. Finally, for ease of use, we put the events and the functions to be activated as arguments, and write the following functions:


function Activemore (event_function,order) {
Activate MyFunction before activating the previous script when the order is negative
var issn=event_function.indexof ("=", 0);
var myevent=event_function.substring (0,ISSN);
var myfunction=event_function.substring (issn+1,event_function.length);
var prefunction= "";
if (eval (myevent)) prefunction= eval (myevent). toString (). Replace (' function anonymous () ', "");

if (order<0) eval (myevent+ "=new Function (myfunction+ ' \\n ' +prefunction)");
else eval (myevent+ "=new Function (prefunction+ ' \\n ' +myfunction)");
}

The above function we first take the argument "event_function" for string analysis, get the event (MyEvent) and the function to activate (MyFunction), and then get the original activated function (prefunction), and finally put multiple functions together to activate. Because we simply remove "function anonymous ()", the prefunction string ends with a "{}" number, but does not affect the program's operation.

Here's an example of a practical application: make all links in a Web page appear without a dotted box.

To achieve this, we add an activation function "blur ()" to all the linked onfocus events. In order to ensure that some links to the original Onfocus event activation function is still valid, we need to use the first Activemore () function, in order to get all the links, we use the Document.body onload event, but also with the Activemore () function:

Make link does not appear dashed box:
function Blurlinks () {
for (i=0;i<>
Activemore ("document.links[" +i+ "].onfocus=if (This.blur) This.blur ()",-1);
}
}
Activemore ("Document.body.onload=blurlinks ()");

Linked onfocus Event (Document.links[i].onfocus) The function to activate is "if (This.blur) This.blur ()", the value of the parameter order is-1 table activates the function before activating their original function The function to be activated by the Document.body.onload event is "blurlinks ()", the value of the parameter order is omitted (or is not a negative number) the table activates its original function and then activates "blurlinks ()".

As you can see from the example, this function is quite simple to use. With the Activemore () function, no matter how many events to activate multiple functions, can be solved with the same syntax!



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.