Compatibility of event events (GO)

Source: Internet
Author: User

window.event problems
Problem Description: Window.event can only run under IE, but not under Firefox, because Firefox event can only be used on the spot where events occur.
Workaround: Add an event parameter to the function that occurred on the incident, and use var myevent = Evt?evt in the function body (assuming the parameter is EVT): (Window.event?window.event:null)

After the Firefox page is called, its Windows.event object is undefined:

    1. if (typeof (window.event) = = ' undefined ')
    2. Alert (' window.event is undefined. ');
    3. Else
    4. Alert (' window.event is defined. ');

The above code is executed in Firefox, with the result: "Window.event is undefined." Results in IE and chrome: "window.event is defined."

1. Passing the event parameter in a function

Pass the event parameter in the function so that we can be compatible with the acquisition of the event of IE and FF, as in the following function:

function _test (EVT)
{
    Gets the source object that triggered the event
    Print the object's Value property
}

So that's what we should do when we're using it:

<input type= ' button ' value= ' click me ' onclick= ' _test (event) '/>

2. Do not pass the event object in a function call

Although there is no parameter passed in the function, this has no effect on IE, because Window.event is a global object and can be called directly anywhere, but not at FF. So here we have to use another way to get it, as follows:

Unction _test2 ()
{
    Arguments. callee. caller. //Get Event object  
    Gets the source object that triggered the event
    Get button code
    Print the object's Value property
    if (window.navigator.userAgent.indexOf ("IE") >=1) { 
       Evt.keycode = 0;
              Evt.returnvalue=false;
           }else{
              Evt.preventdefault ();
           }
}

Then, we can use the same time as normal function call, directly enter the function name, as follows:

<input type= ' button ' value= ' click me2 ' onclick= ' _test2 () '/>

Event.srcelement problems
Problem description: Under IE, even object has Srcelement property, but no target property; Under Firefox, even object has the target property, but there is no srcelement attribute.
Workaround: Use Srcobj = event.srcelement? Event.srcElement:event.target;

Compatibility of event events (GO)

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.