Raphael Jeger
Raphael Jeger

Reputation: 5232

Use jQuery with $ and jQuery as prefix

In a very big project, we have several js-files with $( and jQuery( used as prefixes. We can't fix those files, because they are used in other projects too.

So is there a possibility to make jQuery respond to both prefixes?

Thanks!

Upvotes: 0

Views: 184

Answers (1)

T.J. Crowder
T.J. Crowder

Reputation: 1075895

By default, jQuery uses both $ and jQuery to refer to its function. It's only if you use noConflict (or load another library) that that changes.

But if you're just loading jQuery and not doing anything to change it, this statement will be true:

console.log($ === jQuery); // "true"

FWIW, if for some reason you need to release the $ symbol in your project, the files you have starting with:

$(function() {

can be modified like this:

jQuery(function($) {

...and all the code within the ready function above can continue to use $, which shadows the global. jQuery passes a reference to its function into ready handlers like that.

Upvotes: 3

Related Questions