开发者

JQuery domready and window ready randomly working?

I've noticed lately that sometimes the domready and window.load does not work. It's like randomly working when entering the page, and or refr开发者_如何转开发eshing.

Say I have:

$(function(){
  $('.hide').hide();
  // disable html5 native validation to let jquery handle 
  $('form').attr('novalidate','novalidate');
});

$(window).load(function(){
  $('.input').click(function(){
      $(this).animate({opacity:0.8});
  }).blur(function(){
      $(this).animate({opacity:1});
  });
});

Sometimes when I load the page, the element is not getting hidden, sometimes it is, the input fields will animate, sometimes not, and both don't necessary fail together. If I refresh the page a few times, it will work.

I always thought that domready will execute as soon as the doms are ready, and window.load will wait until everything on the page is rendered ready? Or is this more bugs from HTML5?

Question is: am I missing something or just misunderstanding something?

Edit: Notably Chromium. I am on Ubuntu, so I would not be surprised if it was a chromium bug.


Be aware that if you have a very complex html structure, it may delay the time for the dom to become ready. The browser probably tries to render the page as quickly as it possibly can, and with a really complex page, it's possible that the rendering will begin and the domready event will trigger, but the browser will quickly render stuff before the specific code that you set up gets triggered.

A block in jQuery domready happens as fast as it can, but if you put, say:

setTimeout(function(){ $().ready(function(){alert('finally');});}, 9000);

That "as fast as it can" is still going to be limited by where the code occurs, in this case after a 9 second timeout.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜