Thomas Clowes
Thomas Clowes

Reputation: 4609

Where to include JS files - does it matter?

A few questions today :)

I am using Codeigniter. I have a header view, containing my tags which contains the universal JS file loads.

It also contains the 'layout' of the page, links etc, sidebar, and then it opens the tag and main content . This is closed in my footer view which is loaded after and 'content' view.

In one particular controller, i get data using a method, pass it to a 'content' view, this then sets this php data to a js var (a small block of inline js), and then i include a page specific js file which uses this data. This is in my body. Is this OK?

Thanks

Upvotes: 2

Views: 1868

Answers (3)

Kevin Hakanson
Kevin Hakanson

Reputation: 42210

Take a look at LABjs & RequireJS: Loading JavaScript Resources the Fun Way. That technique really helps IE7, which can't load scripts in parallel.

Upvotes: 0

T.J. Crowder
T.J. Crowder

Reputation: 1074495

For a script block creating a var from a literal rather than downloading a JavaScript file or doing significant processing, no, it's not going to matter much if at all. But unless you care where the var is declared, doing it at the bottom of the body tag as Pointy suggested is still probably your best bet. Even though you're not doing anything, there's the handoff from the HTML parser/renderer to the JavaScript interpreter to consider, which while trivial is, I suppose, non-zero...

Upvotes: 1

Pointy
Pointy

Reputation: 413737

Modern "best practice" advice is to include your Javascript files at the end of the <body>, if possible. That lets your content arrive and render without your Javascript execution slowing the browser down.

Sometimes that's problematic - for example, some server-side frameworks drop little bits of Javascript around page elements, and those might have dependencies on Javascript libraries.

Upvotes: 5

Related Questions