javascript - window.onload function issue - Stack Overflow

I've noticed some bug in window.onload function. (Maybe it's my wrong) The problem is when I

I've noticed some bug in window.onload function. (Maybe it's my wrong) The problem is when I used following simple function, it worked on all browsers but Chrome.

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

Then just for interest, tried this one too:

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

In all above cases, Chrome's dev tools gave me this error message:

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

I've resolved this error with following code.

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

But now want some explanation, why didn't work first 2 piece of code?

I've noticed some bug in window.onload function. (Maybe it's my wrong) The problem is when I used following simple function, it worked on all browsers but Chrome.

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

Then just for interest, tried this one too:

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

In all above cases, Chrome's dev tools gave me this error message:

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

I've resolved this error with following code.

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

But now want some explanation, why didn't work first 2 piece of code?

Share Improve this question edited Dec 24, 2011 at 2:59 ziesemer 28.7k9 gold badges88 silver badges95 bronze badges asked Dec 24, 2011 at 2:56 Tural AliTural Ali 23.3k18 gold badges81 silver badges133 bronze badges 4
  • 1 Don't know about the second example, but that first block of code may be requesting an element that doesn't exist yet. – Jeffrey Sweeney Commented Dec 24, 2011 at 3:01
  • I cached name before working with it, var name=$("#name"); and again, on all other browsers it worked only CHROME gave error – Tural Ali Commented Dec 24, 2011 at 3:02
  • What do you mean by caching? I'm not a JQuery expert, but if $() functions are essentially getElementById() functions, the name element may not have loaded into the DOM yet. Why it is exclusive to chrome is a puzzler though. Do you put your scripts at the bottom of the page by the way? – Jeffrey Sweeney Commented Dec 24, 2011 at 3:06
  • yes the code is at the bottom. I told you that THIS SCRIPT WORKED ON ALL OTHER BROWSERS. imo there is no syntax error. – Tural Ali Commented Dec 24, 2011 at 3:09
Add a ment  | 

1 Answer 1

Reset to default 4

I think that this might be down to a global variable called name. If you call name something different, name1, it works in chrome.http://jsfiddle/R2PuZ/1/

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744281702a4566601.html

相关推荐

  • javascript - window.onload function issue - Stack Overflow

    I've noticed some bug in window.onload function. (Maybe it's my wrong) The problem is when I

    7天前
    60

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信