Skip to main content

Bug Tracker

Side navigation

#8818 closed enhancement (patchwelcome)

Opened April 08, 2011 07:30AM UTC

Closed April 10, 2011 06:24AM UTC

Last modified April 10, 2011 05:49PM UTC

.contains(window, document) throws an exception

Reported by: TiTi Owned by: TiTi
Priority: low Milestone: 1.next
Component: selector Version: 1.5.2
Keywords: needsreview Cc:
Blocked by: Blocking:
Description

Hi,

I've notice the following command :

jQuery.contains(window, document);

throws a TypeError: a.compareDocumentPosition is not a function

Of course window is not a DOM node, so arguments are not correct, thus there is currently nothing wrong with .contains()

But I think it could be useful to check if a document is inside a particular window.

This is why I'm submitting this as an enhancement.

Thx!

Attachments (0)
Change History (5)

Changed April 08, 2011 01:56PM UTC by addyosmani comment:1

_comment0: Thanks for taking the time to contribute to the jQuery project! Please provide a reduced test case on http://jsFiddle.net that reproduces the issue experienced to help us assess your ticket! \ \ Additionally, test against the `jQuery (edge)` version to ensure the issue still exists.1302271111197590
owner: → TiTi
status: newpending

Thanks for taking the time to contribute to the jQuery project! Please provide a reduced test case on http://jsFiddle.net that reproduces the issue experienced to help us assess your ticket! This will also assist in help build a case for this being considered as an enhancement, should we experience the same behaviour.

Additionally, test against the jQuery (edge) version to ensure the issue still exists.

Changed April 08, 2011 02:17PM UTC by TiTi comment:2

status: pendingnew

http://jsfiddle.net/JGSxd/

Hum, looks like this is a FireFox 4 issue ?

It works in Opera 11, IE9, Chrome 10 (no alert).

Changed April 10, 2011 06:24AM UTC by addyosmani comment:3

component: unfiledselector
priority: undecidedlow
resolution: → patchwelcome
status: newclosed

Thanks for replying back with a test case. Further information on compareDocumentPosition (which is used for one of the tests performed by .contains()) can be found here: https://developer.mozilla.org/en/DOM/Node.compareDocumentPosition.

Also, the documentation for jQuery.contains() says:

>jQuery.contains( container, contained )\\\\

>container The DOM element that may contain the other element.\\\\

>contained The DOM node that may be contained by the other element.

As you correctly point out, window is not a DOM node and so an exception here would be raised.

As for this coming in as an enhancement, I'm having a hard time trying to visualize a real-world scenario where needing to test that a document belongs to a particular window would be needed (to the extent that a lot of people would use this feature if included in core).

I believe this is an enhancement that *should* be possible to turn into a plugin outside of core itself. I'd recommend trying to do this and seeing just how well it's received or widely it's used. If you can prove demand, we'd certainly be open to considering it.

Changed April 10, 2011 02:01PM UTC by TiTi comment:4

This is somewhat related to tickets #8010 and #4348: iframes usage.

Small demo with a single iframe: http://jsbin.com/esane5

You could imagine the same with popups [window.open()];

Anyways I submitted this idea because I found it weird that .contains() works with all nodes but not at top level (window/document).

Turns out it fails only in firefox...

As my latest demo shows, you might wan't to use it if you manage several iframes / window. Indeed I don't think this will interest a lot of people, but I can't determine how much people would use this feature...

Changed April 10, 2011 05:49PM UTC by addyosmani comment:5

keywords: → needsreview