Solve an undefined problem with the window.event hint in the JS method in the WebKit browser

Source: Internet
Author: User

This is actually a browser compatibility problem, the root of Baidu in a large pile, briefly said that the event object in IE is a global variable, so where all can be used, but the WebKit kernel browser does not exist in this global variable event, but in the form of an implicit local variable (after the text will be described in detail ).

function myfunc (param) {    alert (window.event);}     // in IE type onclick= "myfunc (' TestIE ')" >    // all normal //WebKit Browser, such as Chrome,  typeonclicklike Firefox = "MyFunc (' Testwebkit ')" >   //  will prompt undefined

Then explain how the WebKit kernel's browser implicitly passes in the event object.

Or the above example, in the WebKit browser, onclick= "MyFunc (' Testwebkit ')" is actually equivalent to this:

onclick (event) {   myfunc (' Testwebkit ');}

In other words, the No. 0 parameter of the OnClick method is actually the one that implicitly passes in the event object.

Then let's look at a call in the JS method: Arguments.callee.caller, where arguments is the parameter set of the method, Callee is the method that the current parameter is in, and caller is the method (or caller) that calls this method.

And then there's the correspondence:

function myfunc (param) {    alert (Arguments.callee);    // myfunc ()    alert (arguments.callee.caller);  // onclick ()    Alert (arguments.callee.caller.arguments[0]);  // Event }    //WebKit Browser, such as Chrome,firefox <input type= "button" onclick= "MyFunc (' Testwebkit ') ">   

In particular, Arguments.callee.caller can continue to pursue the caller, For example, Arguments.callee.caller.arguments.callee.caller is the pursuit of the first level. Why this is especially true, because there are times when you encounter a custom label, such as the case, if there are encapsulation and JS-related methods, The following may be the case:

onclick (event) {  Tagcommand () {     ///// In some cases, multi-layered nested      myfunc (' Testwebkit ');}  }

If you want to get the event object in the MyFunc method at this point, you need to go up to Level 2 and use arguments.callee.caller.arguments.callee.caller.arguments[0] to get it.

Solve an undefined problem with the window.event hint in the JS method in the WebKit browser

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.