I almost always use the latter 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.
-
What do you mean by inline JavaScript?BoltClock– BoltClock2011-04-09 20:49:05 +00:00Commented Apr 9, 2011 at 20:49
-
1Why do you want to manipulate the DOM before it's ready?Raynos– Raynos2011-04-09 20:51:05 +00:00Commented Apr 9, 2011 at 20:51
-
@BoltClock: instead of using jQuery's DOM ready event, placing the Java Script right after the required DOM elements.JCOC611– JCOC6112011-04-09 20:51:09 +00:00Commented Apr 9, 2011 at 20:51
1 Answer
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.