#365 bug
Michael Morris

Element#getStyle problem with IE 6 & 7

Reported by Michael Morris | September 30th, 2008 @ 08:21 PM | in 1.7

I ran across this while using the Element#clonePosition function - IE will through an error about the style being null or not an object on the first call to Element#clonePosition and not on subsequent ones. Frustratingly I couldn't catch the error in outside code so I opened up Prototype itself and looked up the following lines in Element#clonePosition

if (Element.getStyle(element, 'position') == 'absolute') {
		  parent = element.getOffsetParent();
		  delta = parent.viewportOffset();

Following a hunch I wrapped this with a try catch block on IE to try the call a second time if an error is raised. That fix works and is the following code.

try {
		// delta [0,0] will do fine with position: fixed elements,
		// position:absolute needs offsetParent deltas
		if (Element.getStyle(element, 'position') == 'absolute') {
		  parent = element.getOffsetParent();
		  delta = parent.viewportOffset();
	} catch (e) {
		// IE fails on first try and succeeds on subsequent tries. Why? Dunno.
		if (Prototype.Browser.IE) {
			if (Element.getStyle(element, 'position') == 'absolute') {
			  parent = element.getOffsetParent();
			  delta = parent.viewportOffset();

The fix worked and doesn't break anything for Firefox 3, 2, Opera 9, or Safari 3. The fix is a bit of a kludge though - the problem it answers needs to be isolated in Element#getStyle for the final fix to the problem.

Comments and changes to this ticket

  • Nick Stakenburg

    Nick Stakenburg September 30th, 2008 @ 08:34 PM

    try catch is not a solution, the real problem is probably that offsetParent returns the html element, you can't get it's style attribute, that's why you get the error.

    It was fixed in the version but the change got reverted with the release, something we'd have to look into again for

  • Michael Morris

    Michael Morris September 30th, 2008 @ 08:52 PM

    I didn't expect try/catch to be a permanent solution - I called it a kludge after all. I looked at the code in Element#getStyle but got lost so I posted the fix I have so far - I did't expect it to be a final.

    Anyway thanks for looking into it.

  • Tobie Langel

    Tobie Langel October 1st, 2008 @ 01:17 PM

    • State changed from “new” to “bug”
    • Milestone set to 1.6.1
  • Christian

    Christian October 18th, 2008 @ 06:31 PM

    Thanks Michael -- this issue was driving me nuts -- I too needed a kludge to hold me over til

  • Andrew Rev

    Andrew Rev November 1st, 2008 @ 01:23 PM

    That's right, Nick Stakenburg, getOffsetParent() off the element with absolute positioning (IE7 in my case) returns HTML element, which later on along the clonePosition call chain calls for its parent, which happens to be a window object. Which, in turn, doesn't have 'style' object, throwing error on calls like this: element.style[style] (ln. #2271 in v1.6.0.3)

    Later on, ln. #2250 may break the code, 'cos offsetParent.getStyle method understandably appears to be undefined.

  • Yaffle

    Yaffle January 1st, 2009 @ 01:12 PM

    And how to solve this problem?

      getOffsetParent: function(element) {
        if (element.offsetParent) return $(element.offsetParent);
        if (element == document.body) return $(element);
    	if(element.tagName.toUpperCase()=='HTML') //for IE6,7
    	  return $(document.body);                //
        while ((element = element.parentNode) && element != document.body)
          if (Element.getStyle(element, 'position') != 'static')
            return $(element);
        return $(document.body);


  • Juriy Zaytsev

    Juriy Zaytsev February 23rd, 2009 @ 12:03 AM

    • Tag changed from elementgetstyle, getstyle, ie6, ie7 to ie6, ie7, needs_patch, needs_tests
    • Assigned user set to “Juriy Zaytsev”
  • Juriy Zaytsev

    Juriy Zaytsev March 23rd, 2009 @ 04:47 AM

    • Milestone changed from 1.6.1 to 1.7
  • Tobie Langel

    Tobie Langel July 24th, 2009 @ 02:26 AM

    • Tag changed from ie6, ie7, needs_patch, needs_tests to ie6, ie7, missing:tests, needs_patch

    [not-tagged:"needs_tests" tagged:"missing:tests" bulk edit command]

  • Tobie Langel

    Tobie Langel July 24th, 2009 @ 02:28 AM

    • Tag changed from ie6, ie7, missing:tests, needs_patch to ie6, ie7, missing:patch, missing:tests

    [not-tagged:"needs_patch" tagged:"missing:patch" bulk edit command]

  • Tobie Langel

    Tobie Langel July 24th, 2009 @ 03:36 AM

    • Tag changed from ie6, ie7, missing:patch, missing:tests to ie6, ie7, missing:patch, needs:tests

    [not-tagged:"missing:tests" tagged:"needs:tests" bulk edit command]

  • Tobie Langel

    Tobie Langel July 24th, 2009 @ 03:37 AM

    • Tag changed from ie6, ie7, missing:patch, needs:tests to ie6, ie7, needs:patch, needs:tests

    [not-tagged:"missing:patch" tagged:"needs:patch" bulk edit command]

  • Tobie Langel

    Tobie Langel July 24th, 2009 @ 12:46 PM

    • Tag changed from ie6, ie7, needs:patch, needs:tests to needs:patch, needs:tests

    [not-tagged:"ie6" not-tagged:"ie7" bulk edit command]

  • Christian

    Christian September 23rd, 2009 @ 06:51 PM

    Just wanted to comment that Yaffle's fix for getOffsetParent works well with no adverse effects in Webkit Gecko, or Opera (9.64).

  • Jon Evans

    Jon Evans November 4th, 2009 @ 06:56 PM

    Yaffle's fix works for me. IE7, bug tickled by window.js (http://prototype-window.xilinus.com/)

  • T.J. Crowder

    T.J. Crowder November 16th, 2009 @ 04:50 PM

    • Assigned user cleared.

    [responsible:none bulk edit command]

  • Tisho Georgiev

    Tisho Georgiev March 2nd, 2010 @ 08:54 AM

    • Tag changed from needs:patch, needs:tests to needs:patch, needs:tests, section:dom
  • Prabhukarthikeyan R

    Prabhukarthikeyan R November 10th, 2011 @ 07:49 PM

    I am facing the same issue while invoking Element.viewportOffset(element) and Yaffle's fix won't help in this case because viewportOffset directly uses element.offsetParent instead of invoking offsetParent. Hence the viewportOffset code should change along with viewportOffset to invoke offsetParent.

  • Jason Westbrook

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

The Prototype JavaScript library.

Shared Ticket Bins