[x]
Posted via EE Mobile

Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again.

Question
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

9.6

Javascript mouse event position IN an image.

Asked by woottond in JavaScript

Tags: javascript


This is more an urgent question than a difficult question. I'm dealing with several different web browsers and I'd like to keep the code as clean as possible. That being said I know you guys to be good at that - so here I am.

What I have is an image on a webpage. I need to capture where the user clicks in the image. I would use an image map, but I need to capture the event with javascript NOT a page refreshing a href. If you know of a way to capture the mouse click position with an image map without a page refresh I'm all ears.

My end goal for this question is being able to get where the user clicked on an image. The rest of the math is not browser specific and calls a php script to generate a new image. (Think mapquest or google map - I've intentionally not viewed their websites as this is a standalone project and I want to do it my way.)

My current solution works for IE (so long as the user doesn't scroll the page down - an unacceptable bug), Mozilla and Safari (although it might share the IE bug).

If the code is nessisary - you can view it live at
www.ninja250.org/map

The important parts are:

Here's how I trigger the function that gets the mouse click position, I wrap an
a href around an image that I want to watch.

<a href="#" onClick="mapClick(event); return false;">
<img src="map.php?lat=0&long=-90&scale=180" id="map_img">
</a>

To get the absolute position clicked within the picture I subtract the pictures
top left position from the position clicked within the page - leaving me with a value between 0 and the images width.

// MOZILLA then IE
mapImage = document.images['map_img'];
xClick = e.pageX ? (e.pageX - getImagePageLeft(mapImage)) : (e.clientX - getImagePageLeft(mapImage));
yClick = e.pageY ? (e.pageY - getImagePageTop(mapImage)) : (e.clientY - getImagePageTop(mapImage));


To get the images positon on the webpage I've used a little script. I'll show the x version as it's the same for the y version. (Remember the NS4 version works fine for mozilla - even when the page is scrolled down, the IE4+ version breaks when
the page is scrolled.

  var x, obj;
  if (NS4) {
    if (img.container != null)
      return img.container.pageX + img.x;
    else
      return img.x;
  }
  if (IE4) {
    x = 0;
    obj = img;
    while (obj.offsetParent != null) {
      x += obj.offsetLeft;
      obj = obj.offsetParent;
    }
    x += obj.offsetLeft;
    return x;
  }



If anyone could help me get the position clicked on the image in a cleaner and more bug free mode I'd love to give you some points.

Thanks all!
Woot.
[+][-]03/14/05 01:31 PM, ID: 13539184Accepted Solution

View this solution now by starting your 30-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

About this solution

Zone: JavaScript
Tags: javascript
Sign Up Now!
Solution Provided By: dakyd
Participating Experts: 2
Solution Grade: A
 
[+][-]07/18/05 09:52 AM, ID: 14467956Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]07/22/05 11:06 AM, ID: 14505160Administrative Comment

Experts Exchange has a courteous staff of administrators who help members get the most out of the website by means of administrative comments like this one.

Start your 30-day free trial to view this Administrative Comment or ask the Experts your question.

 
 
Loading Advertisement...
20091111-EE-VQP-92