Skip to main content

Bug Tracker

Side navigation

#8405 closed enhancement (patchwelcome)

Opened February 28, 2011 07:18PM UTC

Closed June 06, 2011 06:44PM UTC

Last modified March 14, 2012 12:40PM UTC

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

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.

Attachments (0)
Change History (12)

Changed February 28, 2011 08:39PM UTC by musicisair comment:1

_comment0: After looking at the source it seem that 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).1298925584278853

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

Changed March 30, 2011 06:57PM UTC by dmethvin comment:2

component: unfiledcore
milestone: 1.next1.7
priority: undecidedlow
status: newopen

Changed April 17, 2011 12:08AM UTC by john comment:3

milestone: 1.71.next

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

Changed May 22, 2011 07:27PM UTC by john comment:4

keywords: → 1.7-discuss

Nominating ticket for 1.7 discussion.

Changed May 22, 2011 09:31PM UTC by rwaldron comment:5

description: 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.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.

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

Changed May 23, 2011 12:30AM UTC by jaubourg comment:6

-1, sub is not the right means to do this. Internal caches will be shared and we know where this would lead.

Changed May 23, 2011 04:18AM UTC by timmywil comment:7

+0,

Changed May 24, 2011 09:14PM UTC by dmethvin comment:8

description: 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.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.

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

Changed June 03, 2011 01:47PM UTC by john comment:9

description: 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.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.

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

Changed June 03, 2011 02:50PM UTC by musicisair comment:10

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.

Changed June 05, 2011 09:17PM UTC by ajpiano comment:11

description: 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.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.

+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

Changed June 06, 2011 06:44PM UTC by rwaldron comment:12

keywords: 1.7-discuss
milestone: 1.next1.7
resolution: → patchwelcome
status: openclosed