8

I have the following line of code:

var newLeftPos = $('span#s' + i).position().left - parseInt($('span#s' + i).css('width'), 10);

It works great in ie6 and upwards but I also need to get it to work for ie5.5. (Let's not argue about that now - I know - but I have no option)

I'm certain it falls on the .position() while I'm testing on ie5.5 with jquery 1.2

How could I do the same in plain javascript? Could "offsetParent" help me here? Apparently ie5.5 supports that.

Thanks in advance.

gen_Eric
  • 223,194
  • 41
  • 299
  • 337
Tuomas
  • 91
  • 1
  • 3
  • 2
    What have you tried so far? Have you examined the source of jquery.js to see how the position() function works? – Kiley Naro Sep 12 '11 at 14:29
  • 2
    I'm _really_ curious who in their right mind is still surfing the internet with IE 5.5, because that means they're probably using Windows 2000, or worse Windows Me. – gen_Eric Sep 12 '11 at 14:53
  • 1
    QuirksMode has an article on finding element positions http://www.quirksmode.org/js/findpos.html – Alex Turpin Sep 12 '11 at 14:58
  • Did you have a look at `getBoundingClientRect()` method? – duri Sep 12 '11 at 14:58
  • jQuery will actually allow you to define [cssHooks](http://api.jquery.com/jQuery.cssHooks/) to allow it to work properly. So if you do firgure it out, you can use it easily. – Andrew Sep 12 '11 at 15:00
  • @Andrew: Just FYI: `cssHooks` is only jQuery 1.4.3+ and the OP is using jQuery 1.2. I have no idea how well jQuery 1.4 will work with IE 5.5. – gen_Eric Sep 12 '11 at 15:02
  • Thanks for the replies everyone. I actually found out about the quirksmode find position article, unfortunately there wasn't a working example. The unfortunate fact is also, that I'm not that much of a js expert that I could just look up the jquery source, and get it right from there.(apparently it starts on line 2877(jq1.2)) That's one of the reason why I'm asking the question here. // Unfortunately the later jquery versions don't work on ie5.5, and even when they do load without errors, many times the methods just wouldn't work, as in this case with position(). Oh well, but thanks anyhow. – Tuomas Sep 12 '11 at 15:14

1 Answers1

4

You are looking for offsetParent, offsetLeft and offsetRight.

As you can see in the link, looks like they are supported even by old granny IE5.5.

Check this text page to see if they are really supported by your browser first.

Then your function should be something like

var span = document.getElementById('s' + i);

var newLeftPos = span.offsetWidth - parseInt(span.style.width);
Jose Faeti
  • 12,126
  • 5
  • 38
  • 52
  • Hey, thanks a whole lot! That certainly does exactly what I asked for. So simple. It was "offsetLeft" which I used, and that gave the right value. For some reason span.style.width returns null, but I'm sure that's much easier problem to solve. Thanks. – Tuomas Sep 12 '11 at 16:38
  • @Tuomas Ahh yes I forgot offsetLeft! :) Anyway while testing I also noticed the same problem. It return the right value only if the style is inline in the html, but not if it's in a separated css file... don't know why at the moment. – Jose Faeti Sep 12 '11 at 16:42