Hi There,
yesterday I released a tiny cross browser zero dependencies library which
aim is to make sandboxes creation easy for any kind of purpose.
One of these purposes is to avoid conflicts between different libraries
thanks to sandbox nature and its clear environment which is both not
affected from other global objects and does not affect them, unless
explicitly.
This lib is called *Elsewhere* and you can find the related post here:
http://webreflection.blogspot.com/2009/07/elsewhere-sandboxes-have-never-been.html

The reason I am writing this email is because as first example I showed how
to include and use jQuery in a way that does not require the noConflict call
and creating a quick bridge to the dollar function sending the
parent.document as default context. So far so good.

The problem is that jQuery uses in different cases the global document as
default context. As example, getElementById is performed, obviously, via
document.getElementById(selector.slice(1))

This simply means that there is no way to specify a default document context
in the entire library. I thought about solutions and here I am with some
suggestion which make sense only if jQuery developers will agree with me
that a default document context could be useful in many cases.

*Avoiding Problems With The Rest Of The Library*
To make my proposal as simple as possible without affecting anything else,
the first suggestion is about a local document variable. Since jQuery is
created in its own scope but document is searched outside this scope (while
window is referenced to speed up the look up) we could simply add another
variable after window variable declaration.

(function(){

var
    // Will speed up references to window, and allows munging its name.
    window = this,
    // Will make default document context more flexible
    document = window.document,
    ...

Above snippet will be just a reference that will cause zero side effects for
the entire lib. What's next? A static public function able to set up that
variable.

// make document var redefinition possible outside this scope
jQuery.setDefaultDocument = function(context){
    document = context || window.document;
};

jQuery.fn = jQuery.prototype = {
    ...

With just these few bytes jQuery will become automatically "sandboxable" and
I do not think there will be any side effects, *BUT* ... there is always a
but, a change over that local variable will change the document for every
instance and I don't know how many problems this could cause ...

At the same time, since jQuery uses Sizzle, the problem could be solved
directly there, in sizzle, since if I am not wrong the variable origContext
can make Sizzle calls "sandboxable" but unfortunately in jQuery 1.3.2 Sizzle
is used to find, but not inside the init method.

Accordingly, to solve latest problem I wonder if this snippet would be the
best one:

// let's say we are in a runtime created iframe scope ...
parent.jQuery = (function(anonymous){
    var document = parent.document,
        init = jQuery.fn.init,
        Sizzle = jQuery.find
    ;
    anonymous.prototype = jQuery.prototype;
    return function(selector, context){
        return init.call(new anonymous, Sizzle(selector, document));
    };
})(Function());

I guess if this is the right way even performances will be the same, isn't
it?
Thanks for any kind of suggestion, idea, comment.

Best Regards,
Andrea Giammarchi

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"jQuery Development" group.
To post to this group, send email to jquery-dev@googlegroups.com
To unsubscribe from this group, send email to 
jquery-dev+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/jquery-dev?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to