Let Firefox support the event object implementation code _ javascript skills

Source: Internet
Author: User
FireFox does not have window. event, so it is very troublesome to compile the event processing function in FireFox. To obtain the event object, you must declare that the first parameter of the time processing function is event. Generally, to be compatible with IE and FireFox, the general event processing method is:

The Code is as follows:


Btn. onclick = handle_btn_click;
Function handle_btn_click (evt ){
If (evt = null) evt = window. event; // IE
// Process the event.
}


This is not a hassle for simple programs.
However, for some complex programs, a write function is not directly linked to the event. if you want to pass the event to this parameter, all methods must pass the event .. this is a nightmare.
The following describes how to solve this problem and how it works.
In JScript, a function call has the func. caller attribute.
For example

The Code is as follows:


Function ()
{
B ();
}
Function B ()
{
Alert (B. caller );
}


If B is called by A, B. caller is
In addition, the function has an arguments attribute, which can traverse the parameters currently executed by the function:

The Code is as follows:


Function myalert ()
{
Var arr = [];
For (var I = 0; I Arr [I] = myalert. arguments [I];
Alert (arr. join ("-"));
}
Myalert ("hello", "world", 1, 2, 3)


Hello-world-1-2-3
(The number of arguments is related to the caller, but not to the parameter definition of the function)
Based on these two attributes, we can get the event object of the first function:

The Code is as follows:


Btn. onclick = handle_click;
Function handle_click ()
{
Showcontent ();
}
Function showcontent ()
{
Var evt = SearchEvent ();
If (evt & evt. shiftKey) // if it is an event-based call and shift is pressed
Window. open (global_helpurl );
Else
Location. href = global_helpurl;
}
Function SearchEvent ()
{
Func = SearchEvent. caller;
While (func! = Null)
{
Var arg0 = func. arguments [0];
If (arg0)
{
If (arg0.constructor = Event) // if it is an event object
Return arg0;
}
Func = func. caller;
}
Return null;
}


In this example, SearchEvent is used to search for event objects. 'event' is the FireFox Event. constructor.
In this example,
SearchEvent. caller is showcontent, but showcontent. arguments [0] is empty. So when func = func. caller, func becomes handle_click.
Handle_click is called by FireFox. Although no parameters are defined, the first parameter is event when called. Therefore, handle_click.arguments [0] is event!
Based on the above knowledge, we can combine prototype. _ defineGetter _ to implement window. event in FireFox:
The following is a simple code .. if you are interested, you can add it (I have modified it)

The Code is as follows:


Related Article

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.