No Name

Navigation

Skip navigation.

Search

Site navigation

Email conversation

FromNo Name
ToMe
Subjectfunction findPosition( obj )
Date6 August 2005 15:40
First of all... thank you!

Googled my way to your site as was looking for a way to the x,y position of
an image, so a user can click on the image and I can then draw a circle over
that image where they clicked.. and obtaining the x,y, cordinates ( of the
image) of where they clicked to be stored and accessed later on.

Using your function

function findPosition( obj ) {
  if( obj.offsetParent ) {
    for( var posX = 0, posY = 0; obj.offsetParent; obj = obj.offsetParent ) {
      posX += obj.offsetLeft;
      posY += obj.offsetTop;
    }
    return [ posX, posY ];
  } else {
    return [ obj.x, obj.y ];
  }
}

works very nicely indeed in firefox.. yet it all falls apart for me in IE.

if (str != '') {
        var X = parseInt(str.substring(str.lastIndexOf('x')+2, str.lastIndexOf('&')));
        var Y = parseInt(str.substring(str.lastIndexOf('y')+2, str.length));
       
     /* get map position on page */
        var objMap = document.getElementById('map');
        document.getElementById('textX').value=parseInt(X);
        document.getElementById('textY').value=parseInt(Y);

        var mapX = findPosX(objMap);
        var mapY = findPosY(objMap);
        var mapHeight = objMap.height;
        var mapWidth = objMap.width;

  var posarray = findPosition(objMap);
  alert(posarray);
}
.....................................................

The alert pops up 0,0 in IE ... yet pops up the correct position of the
image in FF

The image (id = map) I have is tucked away way down the page in tables...
and also inside a form. 

e.g.

tables tables tables...

<td width="360" height="300" rowspan="2"> 
<form action="self.php method="get">
<input type="image" id="map" src="images/west_sussex.gif border="0"
height="300" width="300" class="image">
</form>
</td>

close tables tables tables...



Should i give up looking for solution and start thinking of another way of
designing the site (abvoiding tables).... or is this finding the position
just not possible for an image element within a form?  THis is day 2 of even
trying to understand javascript for me and would really appreciate your
comments!

Many thanks
FromMe
ToNo Name
SubjectRe: function findPosition( obj )
Date7 August 2005 00:56
[snip],

> The alert pops up 0,0 in IE ... yet pops up the correct position of
> the image in FF

How about Opera? If you can run FF and IE you can certainly run Opera too. I
am an Opera user myself. It always irritates me when people talk about IE
and FF but ignore Opera like it doesn't exist. Please test your example in
Opera and tell me if it fails in that as well. If you have access to other
platforms (eg Mac/Linux/Unix), please test in Konqueror/Safari/iCab 3/IE Mac
and let me know if it fails in any of them as well.

based on what you sent me, it works for me in IE 6. Can you send me the URL
of the page online where it fails? because I cannot see any reason why it
should fail, and my simple tests with your code worked.

The good news for me is that it also works in Opera, but that still doesn't
explain why it failed for you in IE.


Mark 'Tarquin' Wilton-Jones - author of http://www.howtocreate.co.uk/
FromNo Name
ToMe
SubjectRe: function findPosition( obj )
Date7 August 2005 07:02
Tarquin

Thanks for replying.. much appreciated....!

Ive uploaded the basic page structure to

[URL]

I'll try out on different browsers..   was not ignoring Opera ( or indeed
the others)  its just IE and FF are the 2 browsers I use personally.. and as
my sloppy tables or something or other caused the halt.. I kind of ground to
a halt at the same time  :) 

My friend uses linux.. I can (in the next few days) get over to his and have
a look at the page from his PC.. not sure what browsers he uses though..

Dont know anyone with a Mac  :)   ( just know "of"  them)

the function findPosition( obj )  is added to the [vector]_jsgraphics.js

along with a clunkier version of yours. ( that also crunches under my IE set
up)

function findPosX(obj)
{
 var curleft = 0;
 if (obj.offsetParent)
 {
  while (obj.offsetParent)
  {
   curleft += obj.offsetLeft
   obj = obj.offsetParent;
  }
 }
 else if (obj.x)
  curleft += obj.x;
 return curleft;
}
function findPosY(obj)
{
 var curtop = 0;
 if (obj.offsetParent)
 {
  while (obj.offsetParent)
  {
   curtop += obj.offsetTop
   obj = obj.offsetParent;
  }
 }
 else if (obj.y)
  curtop += obj.y;
 return curtop;
}

function findPosition( obj ) {
  if( obj.offsetParent ) {
    for( var posX = 0, posY = 0; obj.offsetParent; obj = obj.offsetParent )
{
      posX += obj.offsetLeft;
      posY += obj.offsetTop;
    }
    return [ posX, posY ];
  } else {
    return [ obj.x, obj.y ];
  }
}

Am using IE6 too.

(now off to download some other browsers to try it out on!)

Hope this helps you help me?

Kind regards
FromMe
ToNo Name
SubjectRe: function findPosition( obj )
Date7 August 2005 13:47
[snip],

> I'll try out on different browsers..   was not ignoring Opera

This is more useful because if _two_ browsers exibit the same failure, it
is often an easy way of working out exactly what is going wrong.

> My friend uses linux.. I can (in the next few days) get over to his and have
> a look at the page from his PC.. not sure what browsers he uses though..

should not be necessary any more, I just got the cause of the problem :)

you are running the code before the page has loaded. IE is buffering the
rendering offscreen, and as a result, it has not yet laid out the page, so
_all_ offsets are at their initial value of 0. If you wait until the page
has loaded, you will find it works properly. This should be as easy as
wrapping it inside an onload function (assuming you are not using any other
onload functions anywhere):

window.onload = function () {
str = location.search;
  ...<snip>...
        circle.paint();
}
}


Tarquin
FromNo Name
ToMe
SubjectRe: function findPosition( obj )
Date7 August 2005 17:24
Tarquin...,

fantastic... thank you.... !

I ended up moving the script to the bottom of my footer for that page.....

I now understand the principle a lot better than when I first started a few
days ago... and have learnt a lot from this exercise.

I appreciate your website and your personal attention to my emails.

With kind regards
This site was created by Mark "Tarquin" Wilton-Jones.
Don't click this link unless you want to be banned from our site.