[jQuery] Prototype compatibility - a different approach

[jQuery] Prototype compatibility - a different approach

<div>

Hi all,

I actually posted this in bug <a href="http://jquery.com/dev/bugs/bug/137/" target="_blank" onclick="return top.js.OpenExtLink(window,event,this)">#137 - Prototype Support Plugin</a>, but then I realized it might be more appropriate on the mailing list, since I'm looking for feedback on this idea.

Is the main issue here making jQuery compatible with Prototype, or just
making it compatible with all other libraries (now including mootools
et al) which use '$' as a shortcut in the global namespace?




I think it's worth pointing out that jquery.js is written well enough that it doesn't <i>have</i>
to map the jQuery function to $, except as a convenience to users, and
for backwards-compatibility with existing plugins.
There is (presumably) no future-compatible way to consistently merge
the output of jQuery and Prototype's '$' functions together -- and this
would remain ugly and messy, in any case. My suggestion would be
providing the option to map jQuery to a different shortcut (like $J,
for example), and not override prototype's $, if this is what the user would prefer.










There are a number of ways this could be implemented:


<ul><li>the user could change the code themselves and remove <tt>var $ = jQuery</tt>. Messy at best, and forces people to serve the uncompressed version.
</li><li>set a constant on the page before loading jQuery, eg <tt>var overWritePrototype = false</tt>. Again, messy and ugly.
</li><li>My favourite: support a 'hash' parameter in the url of the jQuery script, eg <tt><a href="http://path.to/jquery.js#$J" target="_blank" onclick="return top.js.OpenExtLink(window,event,this)">http://path.to/jquery.js#$J
</a></tt> .
</li></ul>


The script could check for such a hash, and if present, map jQuery to the specified shortcut instead of $.
This is an example implementation (which works in firefox at least):



<pre>new function(){
var fileName, hash;
try { (null).fails }
catch(ex) { fileName = ex.fileName};
hash = /#(.*)$/.exec(fileName);
window[hash ? hash[1] : '$'] = jQuery;
};
</pre>


...which would fall back to the same effect as <tt>var $ = jQuery;</tt> .

This seems like a good idea to me, but there might be something important I haven't considered.
- henrah
</div>
_______________________________________________
jQuery mailing list
discuss@jquery.com
http://jquery.com/discuss/