Solve the [trouble using event] Problem in FireFox

Source: Internet
Author: User

Writing event processing functions in FireFox is troublesome.
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

If (window. addEventListener)
{
FixPrototypeForGecko ();
}
Function FixPrototypeForGecko ()
{
HTMLElement. prototype. _ defineGetter _ ("runtimeStyle", element_prototype_get_runtimeStyle );
Window. constructor. prototype. _ defineGetter _ ("event", window_prototype_get_event );
Event. prototype. _ defineGetter _ ("srcElement", event_prototype_get_srcElement );
}
Function element_prototype_get_runtimeStyle ()
{
// Return style instead...
Return this. style;
}
Function window_prototype_get_event ()
{
Return SearchEvent ();
}
Function event_prototype_get_srcElement ()
{
Return this.tar get;
}

Function SearchEvent ()
{
// IE
If (document. all)
Return window. event;

Func = SearchEvent. caller;
While (func! = Null)
{
Var arg0 = func. arguments [0];
If (arg0)
{
If (arg0.constructor = Event)
Return arg0;
}
Func = func. caller;
}
Return null;
}
</Body>

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.