Let Firefox support event object implementation code_javascript skills
Usually in order to be compatible with IE and FireFox, the general event processing method is:
btn.onclick=handle_btn_click;
function handle_btn_click(evt){
if(evt==null)evt=window.event;//IE
//Handle events.
}
For simple programs, this is not troublesome.
But for some complex programs, a certain function is not directly linked to the event at all. If you want to pass event into this parameter, then all This is simply a nightmare.
The following is a method and principle to solve this trouble.
In JScript, the function call has the attribute func.caller .
For example
function A()
{
B();
}
function B()
{
alert(B.caller);
}
If B is A calls, then B.caller is A
In addition, the function has an arguments attribute. This attribute can traverse the parameters of the current execution of the function:
function myalert()
{
var arr=[];
for(var i=0;i
alert(arr.join("-"));
}
myalert("hello","world ",1,2,3)
will display hello-world-1-2-3
(the number of arguments is related to the caller, and has nothing to do with the parameter definition of the function. Relationship)
According to these two attributes, we can get the event object of the first function:
btn.onclick=handle_click;
function handle_click()
{
showcontent();
}
function showcontent()
{
var evt=SearchEvent();
if(evt&&evt.shiftKey)//If it is an event-based call 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 it is the event object
return arg0;
}
func= func.caller;
}
return null;
}
This example uses SearchEvent to search for event objects. Where 'Event' is FireFox's event.constructor.
When this example is run,
SearchEvent.caller is showcontent, but showcontent.arguments[0] is empty. So when func=func.caller, func becomes handle_click.
handle_click is called by FireFox, although it is not defined parameters, but when called, the first parameter is event, so handle_click.arguments[0] is event!
Based on the above knowledge, we can combine prototype.__defineGetter__ to implement window.event under FireFox:
A simple code is given below. If you are interested, you can add it (I have already modified it)
Javascript and JScript are also different. The former is a client-side script, while the latter is a server-side script and is supported by the server like VBScript.
Of course, I am not here to talk about the difference between the two, but just to let myself know. My current level (not average)...
If I only give the above code, I believe that friends who are just starting to work on compatibility will find it difficult to understand the use of these codes
Here I will explain it Let’s first explain the usage of the above codes
Let’s first look at the explanations of the two methods __defineGetter__ and __defineSetter__:
1. Getter is a method of getting the value of an attribute, and Setter is A method of setting the value of a property. You can define getter and setter methods for any predefined core object or user-defined object, thereby adding new properties to existing objects.
2. When can new attributes be added to objects and events?
1. Define when the object is initialized
2. After the object is defined, add definitions through the __defineGetter__ and __defineSetter__ methods of Object
For detailed usage, please see the explanation of __defineGetter__ and __defineSetter__ here (Address: http://anbutu.javaeye.com/blog/post/194276)
So we saw that in the FixPrototypeForGecko() function Attributes are added to three objects respectively, of course, the objects are added under FF:
HTMLElement adds the "runtimeStyle" attribute, and the attribute value is the value returned by the element_prototype_get_runtimeStyle function
window adds the "event" attribute , the attribute value is the value returned by window_prototype_get_event
Event adds the "srcElement" attribute, and the base attribute value is the value returned by the event_prototype_get_srcElement function
In this way, we have expanded new attributes for these three objects under FF
So we execute the FixPrototypeForGecko() process after judging whether the browser is FF. At this time, these three objects have new attributes under FF
So when we click on the DIV tag, we see in the pop-up window When we reach the words "[object HTMLDivElement]", it also means that we have successfully added the event attribute to the window object
if(window.addEventListener) {
FixPrototypeForGecko();
alert(window.event.srcElement)
}
You can Seeing the element_prototype_get_runtimeStyle process and the event_prototype_get_srcElement process, the returned values can be easily understood
Let’s take a look at how the window_prototype_get_event() process returns events
The return value of the process is the result of the SearchEvent() process. Take a look at this process
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||arg0.constructor==MouseEvent)
if(arg0.constructor ==Event||arg0.constructor==MouseEvent || (typeof(arg0)=="object" && arg0.preventDefault && arg0.stopPropagation))
return arg0;
}
func=func. caller;
}
return null;
}
To understand this process, you must first understand two methods: caller and arguments (there are corresponding explanations in the article)
Here we will explain the constructor attribute again. What is returned is the creator of the corresponding object of the object
In the while loop, alert(func) we can see the return of func.caller, the last return This is our mouse click event
Because the handle_click() process is executed after we click on the div, so the final func.caller is our click event. At this time, funcj is handle_click(), so it is quite So handle_click.caller, of course the caller of handle_click is of course the onclick event, which is [MouseEvent]
You can see that I added a condition in if(arg0.constructor==Event||arg0.constructor==MouseEvent) , because the current result of arg0.constructor is MouseEvent
After seeing this, I believe everyone knows how to write events in FF
Finally, let’s talk about "addEventListener" to register the event method for the object
<script> <BR>function addObjectEvent(objId,eventName,eventFunc) <BR>{ <BR>var targetObj = document.getElementById(objId); <BR>if(targetObj) <BR>{ <BR>if(targetObj.attachEvent) <BR>{ <BR>targetObj.attachEvent(eventName,eventFunc); <BR>} <BR>else if(targetObj.addEventListener) <BR>{ <BR>eventName = eventName.toString().replace(/on(.*)/i,'$1'); <BR>targetObj.addEventListener(eventName,eventFunc,true); <BR>} <BR>} <BR>} <BR>function test1() <BR>{ <BR>alert('test1'); <BR>} <BR>function test2() <BR>{ <BR>alert('test2'); <BR>} <BR></script>
<script> <BR>addObjectEvent('hi','onclick',test1); <BR>addObjectEvent('hi','onclick',test2);//先执行test2(队列) <BR></script>

JavaScript core data types are consistent in browsers and Node.js, but are handled differently from the extra types. 1) The global object is window in the browser and global in Node.js. 2) Node.js' unique Buffer object, used to process binary data. 3) There are also differences in performance and time processing, and the code needs to be adjusted according to the environment.

JavaScriptusestwotypesofcomments:single-line(//)andmulti-line(//).1)Use//forquicknotesorsingle-lineexplanations.2)Use//forlongerexplanationsorcommentingoutblocksofcode.Commentsshouldexplainthe'why',notthe'what',andbeplacedabovetherelevantcodeforclari

The main difference between Python and JavaScript is the type system and application scenarios. 1. Python uses dynamic types, suitable for scientific computing and data analysis. 2. JavaScript adopts weak types and is widely used in front-end and full-stack development. The two have their own advantages in asynchronous programming and performance optimization, and should be decided according to project requirements when choosing.

Whether to choose Python or JavaScript depends on the project type: 1) Choose Python for data science and automation tasks; 2) Choose JavaScript for front-end and full-stack development. Python is favored for its powerful library in data processing and automation, while JavaScript is indispensable for its advantages in web interaction and full-stack development.

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Zend Studio 13.0.1
Powerful PHP integrated development environment

Notepad++7.3.1
Easy-to-use and free code editor
