Call jQuery on (window) .load and the passing variable for the "No Conflict" code

I recently learned a very handy trick that allows you to pass $ in jQuery functions so that you all contain code in No Conflict mode. The advantage is that you can write all the contained code with "$" instead of "jQuery".

This code works fine ...

jQuery(document).ready(function( $ ) { // My code }); 

This code does not work ...

 jQuery(window).load(function( $ ){ // My code }); 

It says: "$ is not a function." How to make it work?

+7
source share
1 answer

Create a (anonymous) self-running function and pass the jQuery object as shown below:

 (function($){ //This functions first parameter is named $ $(window).load(function(){ // Your code }); })(jQuery); //Passing the jQuery object as a first argument 
+15
source

All Articles