Skip to main content

Bug Tracker

Side navigation

#137 closed feature (worksforme)

Opened August 26, 2006 01:06AM UTC

Closed November 30, 2006 09:13PM UTC

Last modified June 19, 2012 01:53AM UTC

Disable $ Function

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

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.

Attachments (0)
Change History (10)

Changed September 07, 2006 09:52AM UTC by john comment:1

milestone: 1.0
owner: johnpaul
version: 1.0

Changed November 13, 2006 11:52AM UTC by anonymous comment:2

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; .

Changed November 17, 2006 09:27PM UTC by john comment:3

description: This needs to be built.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.
milestone: → 1.1
summary: Prototype Support PluginDisable $ Function
version: → 1.1

Changed November 17, 2006 09:28PM UTC by john comment:4

owner: pauljohn

Changed November 26, 2006 12:13AM UTC by brandon comment:5

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.

Changed November 30, 2006 12:09AM UTC by andrea ercol comment:6

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?

Changed November 30, 2006 03:26AM UTC by aaron.heimli comment:7

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.

Changed November 30, 2006 05:22PM UTC by Alex Cook comment:8

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.

Changed November 30, 2006 09:13PM UTC by joern comment:9

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.

Changed June 19, 2012 01:53AM UTC by anonymous comment:10

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