Skip to main content

Bug Tracker

Side navigation

#10267 closed bug (fixed)

Opened September 13, 2011 08:26PM UTC

Closed September 19, 2011 07:43PM UTC

Last modified March 14, 2012 07:25AM UTC

IE8 and window is(':visible') crashes

Reported by: js@dustingraham.com Owned by: timmywil
Priority: low Milestone: 1.7
Component: css Version: 1.6.4rc1
Keywords: Cc:
Blocked by: Blocking:
Description

It appears that in IE8 $(window).is(':visible'); crashes.

http://jsfiddle.net/34BMT/

It probably shouldn't crash.

This seems to be the code where it crashes:

    jQuery.expr.filters.hidden = function( elem ) {
        var width = elem.offsetWidth,
            height = elem.offsetHeight;

        return (width === 0 && height === 0) || (!jQuery.support.reliableHiddenOffsets && (elem.style.display || jQuery.css( elem, "display" )) === "none");
    };

Wondering if after the reliable offsets part we can do a check to see if elem.style exists, if it doesn't exist, then there is a good chance that it's the window itself, which "should" always be visible.

So, adding (elem.style && ...) would result in always returning "false" for the "hidden" check meaning the window is always "visible." (Since elem.style is undefined.)

        return (width === 0 && height === 0) || (!jQuery.support.reliableHiddenOffsets && (elem.style && (elem.style.display || jQuery.css( elem, "display" )) === "none"));
Attachments (0)
Change History (9)

Changed September 14, 2011 10:52PM UTC by dmethvin comment:1

owner: → js@dustingraham.com
status: newpending

Hmmm. The docs are pretty clear that :visible selects elements. What would be the correct value to return here, or are you requesting that it just return a nonsense value without throwing an error? If the window is not visible (e.g., in a browser tab not being shown) should it return false? Should $(new Object).is(":visible") also return false since it is never visible? Why would someone be trying to do this?

Changed September 15, 2011 02:12AM UTC by js@dustingraham.com comment:2

status: pendingnew

The docs also say that $(...) selects elements. So, since $(window) returns cleanly, it seems logical that $(window).is(':visible') should also return cleanly, instead of throwing a javascript error in IE8 since window.style is undefined and then window.style.display crashes IE8.

And your suggestions about the tabs doesn't seem logical, since $(...).is(':visible') doesn't take tabs into account for other elements.

And the time it might be used is if you have some things in an array, perhaps you're not sure what they're going to be, and you want to check if they're visible before doing something.

var things = [window, document, '#title', '.blocks'];

$.each(things, function() {

var items = $(this).is(':visible');

$('#result').after('<p>Vis: ' + items.count + '</p>');

});

So, taking another look, since visible is the opposite of hidden, it seems like $(window).is(':hidden'); should always return false.

Anyways, I just submitted this because I had to work around the problem and someone mentioned that I shouldn't have to work around $(window).is(':visible'); crashing IE8.

Here was my workaround

var visible = !!this[ 'setTimeout' ] || $(this).is(":visible");

Since "this" could be a link, or a div, or the document, or the window, we know that the window is the only element that has the setTimeout method, so that's how we check to see if it's a window first.

Just wanted to help out.

Changed September 15, 2011 02:16AM UTC by js@dustingraham.com comment:3

Replying to [comment:2 js@…]:

var things = [window, document, '#title', '.blocks']; $.each(things, function() { var items = $(this).is(':visible'); $('#result').after('<p>Vis: ' + items.count + '</p>'); });

Oops. I was changing things around and that was an old copy/paste. You get the point though...

var things = [window, document, '#title', '.blocks'];
$.each(things, function() {
  var visible = $(this).is(':visible');
}

Changed September 15, 2011 02:18AM UTC by js@dustingraham.com comment:4

Replying to [comment:2 js@…]:

Here was my workaround var visible = !!this[ 'setTimeout' ] || $(this).is(":visible");

Sorry, I can't edit my posts. :( Not used to the WikiFormatting.

var visible = !!this[ 'setTimeout' ] || $(this).is(":visible");

Changed September 15, 2011 01:29PM UTC by timmywil comment:5

component: unfiledcss
milestone: None1.7
owner: js@dustingraham.comtimmywil
priority: undecidedlow
status: newassigned

This will be fixed in 1.7. #10178 was a similar situation.

Changed September 15, 2011 01:32PM UTC by timmywil comment:6

However, the fix for this will not actually check if a window or tab is visible, but it will not throw an error. Technically, a window is always visible whether it has focus or not.

Changed September 19, 2011 07:43PM UTC by timmywil comment:7

resolution: → fixed
status: assignedclosed

Call .is(:visible) on the window or document does not thrown an error in IE. Fixes #10267.

Changeset: 76a84fba94e0fd32b1a6612c71ef09dee39f6666

Changed November 23, 2011 02:33PM UTC by anonymous comment:8

Could not reproduce.

Changed March 14, 2012 07:25AM UTC by anonymous comment:9