What’s the difference between using this
jQuery(function(){
jQuery('ul.sf-menu').superfish();
});
and just using this?
jQuery('ul.sf-menu').superfish();
It generates the same results, so far as I can tell, but most official code references suggest the former. Can someone explain?
I read through the post What does (function($) {})(jQuery); mean?, but I’m still not certain what the difference is.
Wrapping any function inside
jQueryor$makes sure that it executes it on document ready and not immediately.This will initialize the plugin on document ready which will ensure the required plugin script is loaded on the page.
Alternatively you can also use this
This will initialize right away once the code is parsed but it might throw an error if the plugin script is not loaded so not recommended.