FireFox JavaScript Global Event object _javascript tips

Source: Internet
Author: User
Tags object model
But there is no such object in Firefox, if there is a function nested call, you need to continue to pass the event, such as the following scene.
Copy Code code 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.target.id);
}

The need to use event in the Test2 method is written in this way. If in some scenario, such as adding new features, need to modify the original Test2 method, need to access the event object, and the original Test2 method signature is Test2 (), no parameter event, then need to modify Test2 () Test2 (event) is very beautiful, Although JavaScript is such a modification, it is the overload of the method, but it also destroys the original method signature.
Is there a global variable such as window.event in Firefox to get an event?
Unfortunately, the Firefox object model is not available, but it can be achieved using workarounds. For example:
Copy Code code 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 the use of document.all to determine whether the Internet Explorer is not good practice, should use useragent to judge, jquery and other class libraries have a good implementation.
So the Test2 method above can be signed without modifying the method:
Copy Code code as follows:

function Test2 () {
var event = GetEvent (TEST2);
Alert (Geteventtarget (event). ID);
}
function Geteventtarget (event) {
if (document.all)
return event.srcelement;
return event.target;
}

Why can I write the GetEvent method and get the event?
Because the original event invocation in the Firefox event model was passed to the method, the GetEvent method can be written to get an event that evokes 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.