Under what conditions can you inline Javascript instead of using $(function() { ... })?
I almost always use the latt开发者_运维问答er in my pages but it seems that you can often not do that if you are just accessing elements that are rendered on the page above the script. Is that true across all browsers? I'm hoping to get the lowest possible latency so it would be nice if it could start executing before the complete DOM ready event is fired as I am force flushing the page as it is generated and my Javascript only ever references already created elements or uses things like jQuery's live & delegate.
I've recently implemented this snippet of code because we dropped IE6 support.
<!--[if IE6]><!-->
jQuery.noConflict(true);
$ = new Function;
<!--<[endif]-->
Because we used $(function() { ... })
consistantly to run any kind of inline javascript this shut down all our javascript code.
You may find this useful. I don't see there being any disadvantage to do this apart from being able to save 11 bytes by inlining the code without the jQuery ready handler.
Let's not forget that you get a free closure from using $(...)
and it allows you to not pollute global scope.
using DOM ready handlers is a great code pattern and that dropping them for the reasons you mention is a micro optimisation that would need to be justified as a bottleneck before removed.
精彩评论