0

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.

3
  • What do you mean by inline JavaScript? Commented Apr 9, 2011 at 20:49
  • 1
    Why do you want to manipulate the DOM before it's ready? Commented 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. Commented Apr 9, 2011 at 20:51

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

6 Comments

Sometimes inline is just needed and you can't do without it.(and inline has a faster response)
@tada why is it just needed? Why does it have a faster response time? Why are you messing with the document before its loaded? Whether it's inline or not doesn't change the overall load time. You've designed your page functionality wrong if you need it to be inline
@Raynos if you have a page that flushes out 4 times during generation over 100ms in 25 ms intervals, it would be nice to start running javascript for the parts of the page that have been sent to the browser. As I understand it, wrapping my code in the jQuery ready handler will only execute after the 100ms have past and the complete page is available on the client.
@spullara how can you be so sure any code you write to handle the DOM in the first flush does not effect the other 3 ? And what do you mean flushing 4 times. So your server side sends the HTML to the client in 4 seperate parts?
@Raynos in the cases that i would use this, i would know. i agree that in general you should start with using $() rather than pre-optimize. i just don't want some of the interactive components that have now been displayed by the browser to be inactive while the rest of the page loads.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.