As $ is undefined in a jQuery( window ).on( 'load', function() { I use $ = jQuery; at the top of this code block and then I can use $ rather than typing jQuery wherever I want to use it like this:
jQuery( window ).on( 'load', function() {
$ = jQuery;
var test123 = $( '#test123' );
});
However I have seen on some websites I use this code on it sometimes can throw a Uncaught TypeError: Assignment to constant variable. message occasionally in the console.
Should I be using this method or is there a better alternative to use $ over jQuery
You can wrap it inside a try-catch.
try{
window.$ = jQuery;
}catch(e){};
$(window).on('load', function(){
var test123 = $('#test123');
});