Bug Tracker

Opened 12 years ago

Closed 12 years ago

Last modified 11 years ago

#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:
Blocked by: Blocking:

Description (last modified by ajpiano)

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 (12)

comment:1 Changed 12 years ago by musicisair

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:

var document = window.document;

We can change that to:

jQuery.document = window.document;

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).

Last edited 12 years ago by musicisair (previous) (diff)

comment:2 Changed 12 years ago by dmethvin

Component: unfiledcore
Milestone: 1.next1.7
Priority: undecidedlow
Status: newopen

comment:3 Changed 12 years ago by john

Milestone: 1.71.next

Let's bring this up for discussion again for 1.7.

comment:4 Changed 12 years ago by john

Keywords: 1.7-discuss added

Nominating ticket for 1.7 discussion.

comment:5 Changed 12 years ago by Rick Waldron

Description: modified (diff)

+0, I would prefer to explore jaubourg's "newInstance" approach

comment:6 Changed 12 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:7 Changed 12 years ago by timmywil


comment:8 Changed 12 years ago by dmethvin

Description: modified (diff)

+0, Not sure how this will help, not sure when .sub is useful.

comment:9 Changed 12 years ago by john

Description: modified (diff)

+0, I think it's probably ok if we don't push for this.

comment:10 Changed 12 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:


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 12 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 12 years ago by Rick Waldron

Keywords: 1.7-discuss removed
Milestone: 1.next1.7
Resolution: patchwelcome
Status: openclosed
Note: See TracTickets for help on using tickets.