-2

I'm looking through someone else's javascript when I came across the jQuery keyword used like this:

(function ($) {

    //stuff to do

})(jQuery);


jQuery(document).on('pageshow', function () {

    //call functions

});

what does the jQuery keyword exactly do? or am I reading this wrong and the word 'jQuery' is just an object that is returned from the script or something?

DannyD
  • 2,732
  • 16
  • 51
  • 73

1 Answers1

1

You are aliasing the jQuery global variable to $ in case it was already defined.

In this case the jQuery variable represents the global variable for the jQuery library that was most likely loaded in a script tag before in the HTML code.

m_vdbeek
  • 3,704
  • 7
  • 46
  • 77
  • This mode is often used in big websites. To ensure that no script will fail when including jQuery. – Reflic Aug 12 '13 at 17:10
  • so, if I wanted to call this script from another script that was also set up using this jQuery alias, how could I do that? thanks – DannyD Aug 12 '13 at 17:10
  • @RDoolabh That depends on the particulars of your script, and is a separate question from your original. – André Dion Aug 12 '13 at 17:11
  • You can use something like `$("#selector");` to invoke a function of the element – blurfus Aug 12 '13 at 17:11
  • Without seeing the code, my guess is that you would have to copy / paste the code from one script into the other because both are anonymous functions and thus can't be called by their name. – m_vdbeek Aug 12 '13 at 17:12
  • well, when I call my function in this script, I call it by using: 'jQuery.NameOfFunction();' does the jQuery keyword again just ensure that the script wont fail as @Reflic said? – DannyD Aug 12 '13 at 17:15
  • 1
    Nope, it will only ensure that no other script which possibly uses the $ as a variable identifier will not fail. – Reflic Aug 12 '13 at 17:16