Solve the problem under Firefox [use event very troublesome] _javascript skill

Source: Internet
Author: User
Writing event-handling functions under Firefox is a hassle.
Because Firefox has no window.event. If you want to get an event object, you must declare that the first parameter of the time processing function is event.

So in order 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
Handle the event.
}
For simple programs, this is not a hassle.

But for some complex programs, a write function is not directly linked to the event. If you want to pass the event to this parameter, then all the methods have to pass the event. This is a nightmare.

Here's a way to solve this problem, and the principle.

In JScript, the invocation of a function has a func.caller this property.
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 a arguments property. This property can traverse the parameters currently executing 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)
You can show 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 properties, we can get the event object for the first function:
Btn.onclick=handle_click;
function Handle_click ()
{
Showcontent ();
}
function Showcontent ()
{
var evt=searchevent ();
if (Evt&&evt.shiftkey)//If the call is based on an event, 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 is the event object
return arg0;
}
Func=func.caller;
}
return null;
}
This example uses Searchevent to search for an event object. Where ' Event ' is the event.constructor of FireFox.
When the example is run, the
Searchevent.caller is showcontent, but showcontent.arguments[0] is empty. So Func=func.caller, Func becomes Handle_click.
Handle_click is called by FireFox, although no parameters are defined, but when invoked, the first argument is event, so Handle_click.arguments[0] is an event!

For the above knowledge, we can combine prototype.__definegetter__ to realize the implementation of window.event under FireFox:

Here's a simple code. Interested in 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.target;
}

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.