Firefox and IE get JavaScript access to event method (recommended) _javascript tips

Source: Internet
Author: User
Tags anonymous

JavaScript gets Event

To start with a simple example, a simple button control is as follows:

<input type= ' button ' name= ' mybtn ' id= ' mybtn ' onclick= ' myFunc () '/>

Then register the event for it, and in this case, how to get an event in JavaScript, especially Firefox. Please see:

<script type= ' text/javascript ' >
function MyFunc () {
  var ev = Window.event | | Arguments.callee.caller.arguments[0]
    , et = ev.srcelement | | ev.target;

  alert (et.tagname);  
}
</script>

No accident, in the IE/FF, the above example will output input, that is, the trigger Click event node label name, ie, the event access here will not say, the focus of the situation under the FF.

Here the arguments.callee.caller.arguments[0] looks long and strange, why in the case of Firefox, this thing is an event?

First of all to understand what Arguments.callee is, caller is what kind of attributes?

Argments.callee is the function body itself, Arguments.callee.caller is the function body's calling function body.

The simple example is as follows:

<script type= ' text/javascript ' >
function A () {
   B ();
}

Function B () {
   alert (b = = Arguments.callee)
   alert (B.caller = = a)
   alert (Arguments.callee.caller = = a)

}
A ();
</script>

Not surprisingly, the example above will output 3 true, indicating the relationship between function B and function A when a () is invoked.

Okay, clear up arguments.callee and caller, and we'll change the original example.

<script type= ' text/javascript ' >
function MyFunc () {
  alert (arguments.callee.caller.toString ())
  var ev = window.event | | arguments.callee.caller.arguments[0]
    , et = ev.srcelement | | ev.target;
}
</script>

We output the Argument.callee.caller function body to see what the difference is between IE and ff.

You can see the IE output as

function Anonymous () {
  myFunc ()
}

FF below output as

function onclick (event) {
  myFunc ();
}

This shows that the direct registration event in the HTML control under the IE/FF performance of different, ie under the definition of an anonymous function, the internal implementation of user-defined functions (MYFUNC), and FF under the

Differently, first FF defines a function with the same name as the node event, here is the onclick event, so it is function onclick, then the event is passed as a parameter, and the MyFunc is executed internally.

So when the event is triggered, in the MyFunc, Argument.callee.caller is pointing to function onclick, of course, Argument.callee.caller.arguments[0] is the event.

Above this firefox and IE get JavaScript access to the event of the method (recommended) is small to share all the content of everyone, hope to give you a reference, but also hope that we support cloud habitat community.

Related Article

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.