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

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-05-16 19:23:56
Original
935 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)
It will 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 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 the event object
return arg0;        }
func=func.caller;
}
return null;
}
This example uses SearchEvent to search for event objects. Where 'Event' is FireFox's event.constructor.
In this example When running,
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, it is called When calling, 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

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.__defineG etter__(" 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.target;
}

함수 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;
    }
    null을 반환합니다.
}

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