FireFox JavaScript global Event object

Source: Internet
Author: User

There is no such object in FireFox. If function nested calls exist, the Event must be continuously passed down, for example, in the following scenario.
Copy codeThe Code is as follows:
<Div style = "background-color: Red; width: 300px; height: 300px;" onclick = "Test (event, this ); "id =" panel "> </div>
Function Test (event, dom ){
Test1 (event );
}
Function Test1 (event ){
Test2 (event );
}
Function Test2 (event ){
Alert(event.tar get. id );
}

To use event in the Test2 method, you must write it as follows. In a certain scenario, for example, to add a new function, you need to modify the original Test2 method and access the event object. The signature of the original Test2 method is Test2 () and there is no parameter event, in this case, we need to modify Test2 () to Test2 (event), which is very unattractive. Although JavaScript-like modification is a method overload, it also destroys the original method signature.
Is there a global variable like window. event in FireFox to get the event?
Unfortunately, FireFox does not have an object model, but it can be obtained using a work und. For example:
Copy codeThe Code is as follows:
Function GetEvent (caller ){
If (document. all)
Return window. event; // For IE.
If (caller = null | typeof (caller )! = "Function ")
Return null;
While (caller. caller! = Null ){
Caller = caller. caller;
}
Return caller. arguments [0];
}

Here, document. all is not good for determining whether it is an IE browser. We should use UserAgent to determine whether it is a good implementation in JQuery and other class libraries.
In this way, the Test2 method does not need to modify the method signature:
Copy codeThe Code is as follows:
Function Test2 (){
Var event = GetEvent (Test2 );
Alert (GetEventTarget (event). id );
}
Function GetEventTarget (event ){
If (document. all)
Return event. srcElement;
Return event.tar get;
}

Why can I write the GetEvent method and get the Event?
In the event model of Firefox, the initial event call passes the event display to the method. Therefore, you can write the GetEvent method to obtain the event that invokes JavaScript.
Screen. width-300) this. width = screen. width-300 "border = 0>

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.