Solve problems with [use Event useful] under Firefox.

xiaoxiao2021-03-06  43

Solve problems with [use Event useful] under Firefox.

Writing an event handler in Firefox is a very troublesome thing.

Because Firefox doesn't have Window.Event. If you want to get an EVENT object, you must declare the first parameter of the time handler function as Event.

So in order to be compatible with IE and Firefox, the general event processing method is:

btn.οnclick = handle_btn_click;

Function Handle_BTN_Click (EVT)

{

IF (EVT == NULL) EVT = WINDOW.EVENT; // IE

// Treat event.

}

This is not trouble for simple programs.

But for some complex programs, a write function is not directly hooked directly. If you want to pass Event into this parameter, then all methods will pass Event to pass .. This is a nightmare.

Here is a method, principle that solves this trouble.

In jscript, the call to the function is a Func.caller this property.

E.g

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 property can traverse the functionality of the functionality:

Function myalert ()

{

Var arr = [];

For (var i = 0; i

Arr [i] = myalert.arguments [i];

Alert (arr.join ("-"));

}

Alert ("Hello", "World", 1, 2, 3)

Can show Hello-World-1-2-3

(ARGUMENTS is related to the caller, and there is no relationship with the parameter definition of the function)

According to these two attributes, we can get the EVENT object of the first function:

btn.οnclick = handle_click;

Function Handle_Click ()

{

Showcontent ();

}

Function showcontent ()

{

Var evt = search ();

If (EVT && EVT.SHIFTKEY) // If it is based on an event-based call, and Shift is pressed

WINDOW.OPEN (Global_helpurl);

Else

Location.href = Global_helpurl;

}

Function seatevent ()

{

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;

}

This example uses Searchevent to search for the EVENT object. Where 'Event' is an Event.Constructor of Firefox.

When this example is run,

Searchevent.caller is ShowContent, but showcontent.arguments [0] is empty. So when func = func.caller, FUNC changes to Handle_Click.

Handle_click is called by Firefox, although the parameters are not defined, the first parameter is Event, so Handle_Click.Arguments [0] is Event!

For the top knowledge, we can combine prototype .__ definegetter__ to implement Window.event in Firefox implementation:

Here is a simple code .. Interest can be added

IF (Window.AddeventListener) {

FIXPROTOTYPEFORGECKO ();

}

Function fixPrototypeforgecko ()

{

HTMLELEMENT.PROTOTYPE .__ Definegetter __ ("RuntimeStyle", Element_Prototype_get_RuntimeStyle);

Window.constructor.prototype .__ definegetter __ ("Event", Window_Prototy_get_event);

Event.prototype .__ definegetter __ ("srcelement", Event_Prototype_get_srcelement);

}

Function element_prototype_get_runtimestyle ()

{

// Return STYLE INSTEAD ...

Return this.style;

}

Function WINDOW_PROTYPE_GET_EVENT ()

{

Return searchevent ();

}

Function Event_Prototy_get_srcelement ()

{

Return this.target;

}

Function seatevent ()

{

// 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;

}

转载请注明原文地址:https://www.9cbs.com/read-52336.html

New Post(0)