Home  >  Article  >  Web Front-end  >  FireFox JavaScript global Event object_javascript tips

FireFox JavaScript global Event object_javascript tips

WBOY
WBOYOriginal
2016-05-16 18:51:361023browse

However, there is no such object in FireFox. If there are nested function calls, Events need to be passed down continuously, such as the following scenario.

Copy code The code is as follows:


function Test(event,dom){
Test1(event) ;
}
function Test1(event){
Test2(event);
}
function Test2(event){
alert(event.target.id);
}

If you need to use event in the Test2 method, you need to write it like this. If in certain scenarios, such as adding new functions, you need to modify the original Test2 method and access the event object, and the signature of the original Test2 method is Test2() without the parameter event, then you need to modify Test2() to Test2(event ) is very unsightly. Although this modification to JavaScript is an overloading of methods, it also destroys the original method signature.
Is there a global variable like window.event in FireFox to get the event?
Unfortunately, it is not available in FireFox’s object model, but it can be obtained using workarounds. For example:
Copy code The 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];
}

Use document.all here It is not good to judge whether it is the IE browser. You should use UserAgent to judge. There are good implementations in libraries such as JQuery.
In this way, the above Test2 method does not need to modify the method signature:
Copy the code The 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.target;
}

Why can you write the GetEvent method to get the Event?
Because in Firefox The initial event call in the event model is to pass the event explicitly to the method, so you can write the GetEvent method to get the event that invokes JavaScript.
Click to Open in New Window
Statement:
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