JavaScript
  Home arrow JavaScript arrow Page 4 - Understanding The JavaScript Event Mod...
Dev Shed Forums 
Administration  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Download TestComplete 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVASCRIPT

Understanding The JavaScript Event Model (part 2)
By: Team Melonfire, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 29
    2002-07-10

    Table of Contents:
  • Understanding The JavaScript Event Model (part 2)
  • Back To Basics
  • How's The Weather Up There?
  • X Marks The Spot
  • Enter The Stalker
  • Of Keys And Clicks
  • A Few Modifications
  • Tonight's Menu
  • Reducing The Crime Rate
  • Endgame

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
     
    ADVERTISEMENT

    Route your faxes to your email inbox. Private, secure fax numbers available from CallWave. Choose your fax number.

    Understanding The JavaScript Event Model (part 2) - X Marks The Spot
    (Page 4 of 10 )

    The "layerX" and "layerY" properties return the horizontal and vertical position, in pixels, of the cursor relative to the layer it is in.

    The following example demonstrates:

    <html>
    <head>
    <script language="JavaScript">
    document.onmousedown = CoordCheck;
    function CoordCheck(e) 
    {
    if((e.x < 10 || e.y < 10) || e.x > 420 || e.y > 60)
    {
    alert ("OUTSIDE!!");
    }
    else 
    {
    alert("INSIDE!!")
    }
    }
    </script>
    </head>
    <body>
    <layer id="tryme" left="10" top="10" width="410" height="50"
    bgcolor="cyan"> Try clicking the mouse outside this layer. Then try
    clicking inside. </layer>
    </body>
    </html>
    In this case, I have a layer of a specific height and width, and a function that detects when and where the mouse is clicked on the document. If the position of the pointer when the mouse is clicked is within the layer, an alert box shows up with an appropriate message; if the pointer is outside the layer, a different message appears.

    The event object generated when the mouse button is clicked is passed to the JavaScript function CoordCheck(), which actually checks the position of the cursor and determines where it is in relation to the layer. I'll be going into the nitty-gritty of how JavaScript's mouse events work a little later; for the moment, concentrate on my use of the "layerX" and "layerY" properties in the example above.

    Now, if you tried this example in Internet Explorer, you probably got hit with a couple of error messages. This is because Internet Explorer doesn't support "layerX" or "layerY", preferring instead to use the "x" and "y" properties. Take a look at this rewrite of the previous example, which demonstrates:

    <html> <head> <script language="JavaScript"> document.onmousedown = CoordCheck; function CoordCheck() { // uncomment the next two lines to see coordinate data // alert ("The x-coordinate is " + event.x); // alert ("The y-oordinate is " + event.y); if((event.x < 10 || event.y < 10) || event.x > 420 || event.y > 60) { alert ("OUTSIDE!!"); } else { alert("INSIDE!!") } } </script> </head> <body> <div style="background-color:cyan; position:absolute; top:10; left:10; width:410; height:50"> Try clicking the mouse outside this layer. Then try clicking inside. </div> </body> </html>
    In addition to these properties, there's also the "screenX" and "screenY" properties, which return the horizontal and vertical position of the cursor relative to the screen. Netscape additionally exposes the "pageX" and "pageY" properties, which returns the horizontal and vertical position of the cursor relative to the page.

    More JavaScript Articles
    More By Team Melonfire, (c) Melonfire


     

       

    JAVASCRIPT ARTICLES

    - Getting Attention with Interactive Effects
    - Interacting with Tooltips and Previews
    - Just-in-Time Information and Ajax
    - Interactive Effects
    - Using Cookies With JavaScript
    - Understanding the JavaScript RegExp Object
    - Controlling Browser Properties with JavaScri...
    - Using Timers in JavaScript
    - Form Validation with JavaScript
    - JavaScript Exception Handling
    - Stringing Things Along
    - Understanding The JavaScript Event Model (pa...
    - Understanding The JavaScript Event Model (pa...
    - An Object Lesson In JavaScript

     
    Accelerating Trading Partner Performance
     
    Competing on Analytics
     
    Cost Effective Scaling with Virtualization and Coyote Point Systems
     
    Five Checkpoints to Implementing IP Telephony
     
    Hosted Email Security: Staying Ahead of New Threats
     




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway