views:

47

answers:

1

I know inline Javascript is frowned upon and with the new on-the-fly Javascript compressors that check for idle/unused function usage and omit the unused code, it seems good practice to have all your Javascript in an external file.

My question is, in situations like FOUC (flash of unstyled content) which usually require little snippets of code right before the closing </body> tag, is there a JQuery resolution that would serve the same purpose, but from a remote Javascript file linked in the <head> of the document?

A: 

Is $(document).ready() what your looking for?

$(document).ready(function() {
    // anything in here will execute when the page has finished loading
});
drs9222
drs9222 is exactly right, however inline Javascript has it's place to reduce the number of http requests. (which often max out at 2 per domain), because often a lot of wait time is the browser waiting for another pipeline to show up. This is especially true of home pages which often have a lot of one of a kind scripts that you don't care if they are cached. There are also good compressors that in addition to compressing your code, will inline it. Same with CSS.
zenWeasel
@zen - The 2 per domain limit is only existent in IE still I believe, though it is to the specification, the other browsers left it behind some time ago.
Nick Craver
@drs9222: That actually seemed to work, I already had most my code in the JQuery document ready wrapper and I added the Fouc prevention code right after it, it seemed to result in the same experience. @zenWeasel: That is true, luckily I only have one Javascript document to append aside from Jquery.
Mohammad