Javascript event object

Source: Internet
Author: User

IE and Firefox process event capture differently.

Ie uses the event as an attribute of the window and uses it through the window. Event method. Firefox uses the event object as a parameter of the function to capture the event. Are you confused?

Don't worry. Let's see the following example.

When you click a position on the page, a dialog box is displayed, showing the coordinates of the click point on the screen.
Document. onmousedown = mousedown;

IE:
Function mousedown (){
VaR locstring = "x =" + window. event. screenx + "Y =" + window. event. screeny;
Alert (locstring );
}

In Firefox:
Function mousedown (ffevent ){
VaR locstring = "x =" + ffevent. screenx + "Y =" + ffevent. screeny;
Alert (locstring );
}

Then, in actual scenariosCodeHow to write?

The following code provides a compatibility mode for IE and Firefox, which can be determined by using a ternary operator (Object detection:
Function mousedown (ffevent ){
VaR theevent = ffevent? Ffevent: window. event;
VaR locstring = "x =" + theevent. screenx + "Y =" + theevent. screeny;
Alert (locstring );
}

The complete code for this application is as follows:
<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en"
Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd>
<HTML>
<Head>
<Title> x/y marks the spot </title>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
<SCRIPT type = "text/JavaScript">
// <! [CDATA [

Function mousedown (ffevent ){
VaR theevent = ffevent? Ffevent: window. event;
VaR locstring = "x =" + theevent. screenx + "Y =" + theevent. screeny;
Alert (locstring );
}

Document. onmousedown = mousedown;
//]>
</SCRIPT>

</Head>
<Body>
</Body>
</Html>

 

Logistics Distribution Network http://wlphw.com/QQ online: 471226865

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.