'Is using $ = jQuery; within jQuery( window ).on( 'load', function() { okay?
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
Solution 1:[1]
You can wrap it inside a try-catch.
try{
window.$ = jQuery;
}catch(e){};
$(window).on('load', function(){
var test123 = $('#test123');
});
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
