문제

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

도움이 되었습니까?

해결책

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.

다른 팁

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...

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top