1

In my project every page loads a cdn jquery script. some use $ and some use jquery. and now I have to use my new script file 'myNewScript.js' in every page.

If I write my function like $(function (){}) it gives error on some pages like

$ is not function

and if I write function like this jquery(function (){}) then it also give error on other page like

jquery is not a function.

I want to know can we use both $ and jquery as variable . like in same script file say 'myNewScript.js' I want to write $(function (){}) and jquery(function (){}).

Every thing is dependent on each other so I can't reverse the loading of the script. I can't think of any solution...

Jai
  • 74,255
  • 12
  • 74
  • 103
thedudecodes
  • 1,479
  • 1
  • 16
  • 37

2 Answers2

1

jQuery should always be available using the name jQuery (note the uppercase Q). So use that:

jQuery(function (){})

You can also wrap your code in an IIFE if you would like to refer to jQuery as $ while dealing with the uncertainty whether the global $ actually refers to it:

(function ($) {
    $(function () {
        // on ready stuff
    });
})(jQuery);
JLRishe
  • 99,490
  • 19
  • 131
  • 169
0

Define jQuery locally for yourself by looking for both variables.

var myJquery = jquery || $;
ryannjohnson
  • 373
  • 1
  • 5
  • 13