Ticket #8405 (closed enhancement: patchwelcome)
Enhance jQuery.sub to work in its own window/document
| Reported by: | musicisair | Owned by: | |
|---|---|---|---|
| Priority: | low | Milestone: | 1.7 |
| Component: | core | Version: | 1.5.1 |
| Keywords: | Cc: | ||
| Blocking: | Blocked by: |
Description (last modified by ajpiano) (diff)
It would be nice if we could pass a DOM Window or Document to jQuery.sub in order to force selector context to any document we want WITHOUT having to $(document).find("selector") or $("selector", document)
Basically, we could then copy jQuery into an iframe or window (window.open) and execute it in its own context without the cost of loading and initializing.
In certain scenarios I could see how support tests may need to be re-run if the document-mode isn't the same.
---
On a side note: any hints on to implementing this right now as a plugin would be much appreciated.
Change History
comment:2 Changed 2 years ago by dmethvin
- Priority changed from undecided to low
- Status changed from new to open
- Component changed from unfiled to core
- Milestone changed from 1.next to 1.7
comment:3 Changed 2 years ago by john
- Milestone changed from 1.7 to 1.next
Let's bring this up for discussion again for 1.7.
comment:4 Changed 2 years ago by john
- Keywords 1.7-discuss added
Nominating ticket for 1.7 discussion.
comment:5 Changed 2 years ago by rwaldron
- Description modified (diff)
+0, I would prefer to explore jaubourg's "newInstance" approach
comment:6 Changed 2 years ago by jaubourg
-1, sub is not the right means to do this. Internal caches will be shared and we know where this would lead.
comment:8 Changed 2 years ago by dmethvin
- Description modified (diff)
+0, Not sure how this will help, not sure when .sub is useful.
comment:9 Changed 2 years ago by john
- Description modified (diff)
+0, I think it's probably ok if we don't push for this.
comment:10 Changed 2 years ago by musicisair
Oh well, it looks like this one isn't going to make it.
I'll make one last stand for it, though...
I suggested using $.sub, but it doesn't really matter to me. I'd just like a document-aware jQuery. Here is my use case:
I've got a in-browser "intranet" browser. This HTML5+CSS3+jQuery browser runs fullscreen in webkit/firefox and it has all of your normal browser bells and whistles - like tabs (via iframes), an address bar, back/forward/refresh buttons, etc. All of the intranet web-pages "initialize" jQuery as follows:
if(!parent !parent.jQuery){ document.write("<script src='jquery.js'></script>"); </script should be escaped in the real world.
} else{
$ = jQuery = parent.jQuery.sub();
}
...which is pretty useful little trick. The draw back is that the intranet iframes can modify the parent's jQuery object...which could potentially break our application. This is mostly fixed by $.sub (though I imagine different document modes could cause issues with $.support, but I'm not sure). The only other obvious issue is that all jQuery selectors need to be aware of the iframe's document, which is doable, sure...but pretty annoying.
So yes, it's an edge-case...but also a really cool way to re-use a script resource without have to re-parse and re-execute it.
comment:11 Changed 2 years ago by ajpiano
- Description modified (diff)
+1, It would be really nice if there was a generic way to always make jQuery calls relative to another document, instead of having to flood context arguments
comment:12 Changed 2 years ago by rwaldron
- Keywords 1.7-discuss removed
- Status changed from open to closed
- Resolution set to patchwelcome
- Milestone changed from 1.next to 1.7
Please follow the bug reporting guidlines and use jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.

After looking at the source it looks like if we made the document a jQuery property instead of a private variable we could make jQuery document-aware relatively easily.
At the top of jquery we've got:
We can change that to:
then update all references to document to point jQuery.document instead and then add the following to jQuery.sub
sub: function(document){ /* ... */ jQuerySubclass.document = window.document || jQuery.document; /* ... */ };There may be some issues with even binding and we may want to make window a jQuery property in order circumvent those issues also.
DaveMethvin pointed out that there may be some memory-leak issues when "remote-controlling" an iframe this way if the iframe is closed or navigated to another page.
Also, Sizzle will need to be updated to allow for setting the default document (context).