Home > Web Front-end > JS Tutorial > Solve the problem of [using events is troublesome] under FireFox._javascript skills

Solve the problem of [using events is troublesome] under FireFox._javascript skills

WBOY
Release: 2016-05-16 19:26:59
Original
947 people have browsed it

Writing event processing functions under FireFox is very troublesome.
Because FireFox does not have window.event . If you want to get the event object, you must declare the first parameter of the time processing function as event.

So in order to be compatible with IE and FireFox, the general event processing method is:
btn.onclick=handle_btn_click;
function handle_btn_click(evt)
{
if(evt==null)evt=window. event;//IE
//Process events.
}
For simple programs, this is not troublesome.

But for some complex programs, writing a certain function is not straightforward at all It is linked to an event. If you want to pass the event into this parameter, then all methods must pass the event back and forth. This is simply a nightmare.

Here is a way to solve this troublesome problem, with Principle.

In JScript, function calls have the attribute func.caller.
For example,
function A()
{
B();
}
function B()
{
alert(B.caller);
}
If B is called by A, then B.caller is A

In addition, the function has An arguments attribute. This attribute can traverse the parameters of the current execution of 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)
Display hello-world-1-2-3
(The number of arguments is related to the caller and has nothing to do with the parameter definition of the function)

According to these two attributes, we can get the first event object of a 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)
}
func=func .caller;
}
return null;
}
This example uses SearchEvent to search for event objects. Where 'Event' is FireFox's event.constructor.
When this example is run,
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, when called, The first parameter is event, so handle_click.arguments[0] is event!

Based on the above knowledge, we can combine prototype.__defineGetter__ to implement window.event under FireFox:

A simple code is given below. If you are interested, you can add it



Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template