Problem with window.onload function

I noticed an error in the window.onload function. (Maybe this is my mistake) The problem is that I used the following simple function, it worked on all browsers, but Chrome.

 var name=$("#name"); window.onload = function(){ name.fadeIn(500); }; 

Then just for fun, I tried too:

 var name; window.onload = function(){ name=$("#name"); name.fadeIn(500); }; 

In all of the above cases, the Chrome dev tools gave me this error message:

 Uncaught TypeError: Object [object Object] has no method 'fadeIn' 

I solved this error with the following code.

 window.onload = function(){ var name=$("#name"); name.fadeIn(500); }; 

But now some explanation is needed, why 2 parts of the code did not work first?

+6
source share
1 answer

I think it could be up to a global variable called name. If you call something else, name1, it works in chrome. http://jsfiddle.net/R2PuZ/1/

+4
source

Source: https://habr.com/ru/post/904422/


All Articles