This code creates a webpage with an html div containing the words ONE TWo THREE. A Java Script function uses the event object's clientX and clientY properties to determine which word the user clicked on and displays it in a message box.
Welcome to Bucaro TecHelp!

Welcome to Bucaro TecHelp!
Maintain Your Computer and Use it More Effectively
to Design a Web Site and Make Money on the Web

[About BTH]  [User Agreement]  [Privacy Policy]  [Site Map]  [Contact Form]  [Advertise on BTH]  [News Feed]

Google
Web
This Site

Where Did the User Click?

Pixel Locations

An image on a computer screen is made up of "pixels" (colored dots). The computer screen itself consists of an array of pixels. The location of every pixel can be described by a pair of digits representing its X location and its Y location. A pixels X location is its distance in pixels from the left side of the screen. A pixels Y location is its distance in pixels from the top the screen.

- If your screen resolution is set to 800 x 600 pixels, a pixel at the center of your screen would be located at X=400, Y=300.

A browser window uses the same coordinate system. Let's say you open a browser window 400 pixels by 400 pixels and center it on your screen. The location of a pixel at the center of the browser window would be X=200, Y=200. That is, the pixel is located 200 pixels from the left side of the browser window and 200 pixels from the top the browser window.

Because the browser window is not maximized, the browser window location and the screen location of the pixel would not be identical. Since we said the browser window was centered on the screen, the screen pixel location would be X=400, Y=300.

Browser Objects

On a webpage you might find buttons, checkboxes, links, text, and so on. Almost everything on a webpage is an "object". Almost all objects can respond to an "event". An event can be triggered by the user. For example, an "onClick" event is triggered when the user clicks a mouse button.

Both the W3C and the Internet Explorer models have an "event" object that is triggered by any event occurring in the browser. Although the properties of the two models differ in a number of ways, both provide properties that you can use to determine the location of an invent in the browser window and on the computers screen.

Properties of the event object
clientXX location in the browser window
clientYY location in the browser window
ScreenXX location on the screen
ScreenYY location on the screen

To experiment with the event object, type the code shown below into Windows Note pad or your favorite plain ASCII text editor and save the file with the name client.htm.

<html>
<head>

<script language="JavaScript">
function position(e)
{
document.forms(0).position.value = e.clientX +
" " + e.clientY;
}
</script>

</head>
<body onmousedown="position(event)">

<form>
<input type="text" name="position"></input>
</form>

</body>
</html>

This code creates a webpage with a text box. A Java Script function named "position" puts the event object's clientX and clientY properties in the text box. In the body tag, the position function is attached to the body's onmousedown event.

When you open the webpage in your browser and click anywhere in the webpage, the browser window pixel location where you clicked will be displayed in the text box.

A simple change to the line of code inside the position function as shown below causes it to display the screen location where you clicked.

document.forms(0).position.value = e.screenX +
" " + e.screenY;

Note: You still have to click inside the browser window. If you click outside the browser window, the body's onclick event can't see the click event (but the Windows operating system received it).

Web Design Sections

RSS Feed RSS Feed

Easy Java Script Code
Easy Slide Show Code
Easy Slide Show Code with Mouseover Pause
Easy Slide Show Code With Linked Slides
Slide Show with Different Size Images
Easy Java Script Timer Code
Easy Java Script Windows
Easy Rollovers
Easy Picture Transition Effects
Easy Moving Popup Code
Easy Picture Zoom Code
Easy Picture Panning Code
Easy Java Script Animation
Easy JavaScript Picture Selector Code
Easy Animated Banners with Java Script
Easy Java Script Form Validation
Easy Drag n Drop Code
Easy Graph Generating Code
Easy Code to Sort a Table by Column
Easy Expanding Menu Code
Web Designer's Reference
Object-Oriented JavaScript
Easy Fading Text Banner Code
Easy Expanding Banner Code
Calendars for Your Website
Date Picker For Your Web Site
Calculators For Your Web Site
Loan Payment Calculator
Length Units Converter
Body Mass Index
Fahrenheit to Celsius Converter
Decimal to Hexidecimal Converter
Easy Code for Screen Tape Adding Machine
Code for Java Script Circle⁄Sphere Calculator
Code for Java Script Cube⁄Box Volume Calculator
Round a Float to 4 Digits to the Right of the Decimal Point
Disable IE8 Accelerators on Your Website
Prevent Automated Form Submissions With Java Script
Make Your Own Graphical Digital Clock
Let Your Web site Visitors Set Font Size
How to Disable the Browser Back Button
Add More Bang to Your Content With Keyword Popup Menus
Put a Music Off Switch on Your Webpage
Java Script Random Password Generator
Password Protection Using the JavaScript Cookie Method
Password Protection Using the Frames Method
Replace Drop-down with Drag-and-drop
Submit Forms Without CGI
Code for a Less annoying Popup Window
Java Script Comparison Operators
Using the Java Script Array Object
Using the Java Script Date Object
Java Script Message Boxes
Java Script Trim Function
Convert Mixed Number to Decimal
How to Shuffle the Deck With Java Script
Web Site Menus : Which Section Am I In?
How Far Did the User Scroll?
Where Did the User Click?
Four Ways to Use Java Script Event Handlers
Create Your Own Database Using Only Notepad : CDV


[Site User Agreement]  [Advertise on This site]  [Search This Site]  [Contact Form]
Copyright©2001-2009 Bucaro TecHelp P.O.Box 18952 Fountain Hills, AZ 85269