0

This may be a math question more than a programming question, but we'll see.

I'm trying to build a simple javascript click tracker that one could add to any website, which will plot its data to a heat map.

The Problem

If I capture the x/y coordinates of a click event on a web page being displayed in a window measuring 1024x768, and try to map that onto a heat map being displayed at 1280x1024 (or any other size), there's a good possibility that the click won't display in the correct location.

If, for example, the content of the web page is centred within a 960px wide div, because the x,y coordinates captured measure from an origin at the top-left corner of the window, the coordinates don't match in physical space.

I could use the difference between the two screen sizes to create an origin in the centre of all screens and measure from there, but then I wind up with the same problem for any content that isn't positioned relative to the centre of the page.

Is there a way to develop a set of absolute coordinates of an element on the page, without knowing the size of the browser window or what's on the page?

I know I could bind the click to the DOM element. That's fine, and I'll probably go that route, but now that I've found this problem, I'm curious as to the possible solution.

andrewheins
  • 788
  • 2
  • 9
  • 17
  • To rephrase, do you want a universal coordinate system that will be consistently applicable whatever dimensions the user has their browser set at? – Jim Blackler Mar 31 '11 at 20:46
  • Wrap the contents of the webpage in a div and base your clicks events off of that. Also keep in mind if the user resizes the window smaller then your web page you will have a scroll bar and you have to account for the scroll offset. – The_asMan Mar 31 '11 at 21:32

1 Answers1

1

jQuery x y document coordinates of DOM object describes a similar problem and has a possible solution. Hopefully that deals with browser behavior correctly.

If that doesn't work, this problem is likely to be tricky to solve. See http://weblogs.asp.net/bleroy/archive/2008/01/29/getting-absolute-coordinates-from-a-dom-element.aspx for how much difficulty Microsoft had in implementing this feature a few years back.

Community
  • 1
  • 1
btilly
  • 43,296
  • 3
  • 59
  • 88