Bug Tracker

Opened 9 years ago

Closed 9 years ago

#14709 closed bug (notabug)

$(window).is(":visible") incorrectly returns false

Reported by: frederic.hamidi@… Owned by: frederic.hamidi@…
Priority: undecided Milestone: None
Component: unfiled Version: 1.10.2
Keywords: Cc:
Blocked by: Blocking:

Description

From jQuery 1.10 onwards, the is() method returns false instead of the expected true when applied to the window object and the :visible selector. This occurs on all the browsers I could test under (Internet Explorer, Firefox, Chrome).

Test case: http://jsfiddle.net/e4HzX/

Steps to reproduce: Print the value returned by $(window).is(":visible").

Expected behavior: is() should return true.

Observed behavior: is() returns false.

Reproduced on:

Last known version with correct behavior:

  • 1.9.1

Change History (5)

comment:1 Changed 9 years ago by dmethvin

Owner: set to frederic.hamidi@…
Status: newpending

Not sure this is a valid use of .is(), since a window is not a DOM element. What is your use case that leads you to call code like that?

comment:2 Changed 9 years ago by frederic.hamidi@…

Status: pendingnew

I'm currently using the jquery.nicescroll plugin in our project. It can augment both the page itself and arbitrary containers, and maintains references to these elements -- or, in the page's case, to window instead of say, the <body> element. This leads to code in the plugin like:

self.jqbind(self.rail, "mouseenter", function() {
    if (!self.win.is(":visible"))
        return false;
    // Show scrollbar widget...
});

Where self.win is a jQuery object that can either contain a DOM element or window. The visibility check fails for window in jQuery 1.10+, so the plugin fails to show its scrollbar widget on mouseenter for the main page only.

I can fix the plugin, and probably also special-case is() for window, but IMHO that would not beat the original (pre-1.10) behavior being restored. I suspect it might not be as easy as I think, though.

comment:3 Changed 9 years ago by dmethvin

I think the original behavior on window has always been undefined, the docs say this:

:visible Selects all elements that are visible in the document. ... Elements are considered visible if they consume space in the document.

Since window is not an element and is also not "in the document" I don't think it fits the description of valid inputs. It's similar to saying $({visible:1}).is(":visible"), we just don't guarantee what that will return. There are a lot of things you can do to elements that you can't do to a window.

I'd recommend changing the plugin logic. Is jquery.nicescroll abandoned? I've been trying to get some response to issues and even volunteered to help, but nothing seems to be going on with it. It's got a bunch of fragile browser sniffs that are causing problems in several environments including IE10/11 and Chrome >31. So jQuery's changed behavior on an invalid input is probably the least of our worries.

comment:4 Changed 9 years ago by anonymous

You have a point, nothing was actually specified for window in the first place. This probably is enough of an edge case that I can live with it.

Maybe the documentation for is() could be updated to say its behavior is undefined for window (and probably also document)?

I'll try a pull request to the plugin once I get this fixed on my side, two months of inactivity are not that much after all.

comment:5 Changed 9 years ago by dmethvin

Resolution: notabug
Status: newclosed
Note: See TracTickets for help on using tickets.