Save the jQuery factory, reuse it in frames, no 2nd network request

Save the jQuery factory, reuse it in frames, no 2nd network request

Yes, there are those who hate frames with all their souls. If you're one of them, you probably won't like this idea at all. But if part of the page never or rarely changes, and you think frames make sense in that situation, you probably won't reject this idea on religious grounds.

Terminology: The jQuery script tag doesn't contain jQuery. It contains the jQuery factory, which looks like this:

(function( window, undefined ) {
    ...
    var
        ...
        // Use the correct document accordingly with window argument (sandbox)
        document = window.document,
        ...
    ...
})( window );

Because the factory is an anonymous, self-executing function, it's immediately thrown away, and the window.jQuery and window.$ it builds have the window and document at self-execution time preserved in its closure variables (the sandbox).

But the same sandbox that protects jQuery in the current document keeps it from being used in other documents, such as the document of a frame. If a frame also wants to load jQuery, it normally must do so with another script tag, even though a network request has already been made for the the exact same jQuery factory. Sure, the request will probably get a Not Modified and the factory will get loaded from cache and reprocessed. But why make the request at all?

So here's my idea. Save the factory. To do so, replace this:

    (function ( window, undefined ) {

with this:

    window.jQueryFactory = window["jQueryFactory-1.9.1"] = function ( window, undefined ) {

and at the bottom of the file, replace this:

    })( window );

with this:

    };
    jQueryFactory( window );

Then later, in the document loaded into the frame, you can say this:

    top.jQueryFactory( window ); // window of the frame.

The same factory that defined jQuery in top can be reused to define jQuery in the frame. The 4 lines above have been tested and I haven't yet hit any problems with the copy of jQuery generated in the frame using the factory originally loaded in top.

This idea is a new feature suggestion for the jQuery team, because it should be available automatically in new releases, without having to edit every new release manually and host an edited version locally. I know the jQuery team is proud of "not polluting the global scope" unnecessarily, but I think it's worth it in this case to define window.jQueryFactory and window["jQueryFactory-1.9.1.min"] (or whatever version it generates).

Now that features are configurable via build, it might be possible to make it a build-time choice. (I don't know jQuery's build system well enough to know whether that's possible.) So particularly if it's configurable, please add saving-the-factory as a build option.