Bug Tracker

Opened 14 years ago

Last modified 8 years ago

#137 closed feature

Disable $ Function — at Version 3

Reported by: john Owned by: paul
Priority: major Milestone: 1.1a
Component: core Version: 1.1a
Keywords: Cc:
Blocked by: Blocking:

Description (last modified by john)

This can be used as a way to use Prototype and jQuery at the same time. The code could look something like this:


Making any instance of '$' that it overwrote revert back to its old value.

Change History (3)

comment:1 Changed 14 years ago by john

Milestone: 1.0
Owner: changed from john to paul
Version: 1.0

comment:2 Changed 14 years ago by anonymous

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 have 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 wants.

There are a number of ways this could be implemented:

  • the user could change the code themselves and remove var $ = jQuery. Messy at best, and forces people to serve the uncompressed version.
  • set a constant on the page before loading jQuery, eg var overWritePrototype = false. Again, messy and ugly.
  • My favourite: support a 'hash' parameter in the url of the jQuery script, eg http://path.to/jquery.js#$J .

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

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

...which would fall back to the safe effect as var $ = jQuery; .

comment:3 Changed 14 years ago by john

Description: modified (diff)
Milestone: 1.1
Summary: Prototype Support PluginDisable $ Function
Version: 1.1
Note: See TracTickets for help on using tickets.