Bug Tracker

Opened 17 years ago

Closed 16 years ago

Last modified 11 years ago

#137 closed feature (worksforme)

Disable $ Function

Reported by: john Owned by:
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:

   jQuery.disableAlias();

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

Change History (10)

comment:1 Changed 17 years ago by john

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

comment:2 Changed 16 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 16 years ago by john

Description: modified (diff)
Milestone: 1.1
Summary: Prototype Support PluginDisable $ Function
Version: 1.1

comment:4 Changed 16 years ago by john

Owner: changed from paul to john

comment:5 Changed 16 years ago by brandon

The hash idea is clever but all it takes is one line of code to make jQuery play nicely with libraries that use the $ alias.

$ = _$;

Now jQuery can be used or jQuery could be aliased to something else with just one line.

var $J = jQuery;

Here is a way to wrap this functionality up in a method (untested).

jQuery.changeAlias = function(alias) {
    $ = _$;
    window[alias] = jQuery;
}:

and used like so:

jQuery.changeAlias('$J');

I'm still not sure that this should go in the core. If it should go into the core, then maybe it should look more like this (just coding off the top of my head, untested):

jQuery.changeAlias = function(alias) {
    if (jQuery.savedAlias)
        window[jQuery.savedAlias[0]] = jQuery.savedAlias[1];
    
    jQuery.savedAlias = null;

    if (window[alias] != undefined)
        jQuery.savedAlias = [alias, window[alias]];
    
    window[alias] = jQuery;
};

Then in the core we should use it to set the '$' alias so that when this method is called it will restore the original alias if it is being used.

comment:6 Changed 16 years ago by andrea ercol

If the incompatibilities between jQuery and the rest of the world are limited to the $, please fix it asap. It's much better to have friends than enemies, and it would be nice to have bridges too, maybe by means of json?

comment:7 Changed 16 years ago by aaron.heimli

One thing I like about the hash idea is that it takes care of the situation where another script/library has initialization code that uses $() and is automatically run onload. As long as jQuery is loaded before the other script, its alias will have already changed by the time the other script's init code is run.

That being said, the only really reliable way I can think of to get the hash is to put an ID (like jQuerySRC or something) on jquery.js's script element (Anonymous' code, as he states, only seems to work in Firefox), which adds another step to the process.

jQuery.changeAlias() is probably easier to deal with overall, though; you just need to be sure to call it before any other code that uses $() runs.

comment:8 Changed 16 years ago by Alex Cook

Having the ability to change the alias if needed should be fine for anyone looking to use the other libraries that use the $ shortcut. I'd vote for .changeAlias, but I'm not sure if it's necessary in the core. Developers would be using this, not many newbies, so perhaps it could be a plugin/option when people build their distro.

comment:9 Changed 16 years ago by joern

Resolution: worksforme
Status: newclosed

If you build your own distribution, you can simply change the alias in the source. I just moved the declartion more to the start of jquery.js, so it can be found easier.

If you expect jQuery to be simply compatible with everything else, a disableAlias function won't help either.

comment:10 Changed 11 years ago by anonymous

js's script element (Anonymous' code, as he states, only seems to work in Firefox), which adds another step to the process. michael kors handbags

Note: See TracTickets for help on using tickets.