Solve the [trouble using event] Problem in FireFox _ javascript skills
Source: Internet
Author: User
Solving the problem of [using event in FireFox is troublesome] It is very troublesome to compile the event processing function in FireFox.
Because FireFox does not have window. event. To get the event object, you must declare the first parameter of the time processing function as event.
To be compatible with IE and FireFox, the general event handling method is:
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
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:
Function myalert ()
{
Var arr = [];
For (var I = 0; I
Arr [I] = myalert. arguments [I];
Alert (arr. join ("-"));
}
Alert ("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:
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:
A simple code is provided below. If you are interested, you can add
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.