RS Shamil
05-29-2009, 07:12 AM
Hello,
I am doing a complete code optimization on my project that I am developing. Currently some pages require external js files for things such as jQuery UI, TinyMCE etc.
Given that each page is called and included in a div on a page, where would be the best place to place the JS files?
tim2718281
05-29-2009, 11:34 AM
Hello,
I am doing a complete code optimization on my project that I am developing. Currently some pages require external js files for things such as jQuery UI, TinyMCE etc.
Given that each page is called and included in a div on a page, where would be the best place to place the JS files?
It's best to put Javascript at the bottom, so it's the last thing loaded.
The reason is that browsers download multiple URLs in parallel, but block parallel downloads while Javascript is being loaded.
squirrelhost
05-29-2009, 11:38 AM
If possible they should be put at the bottom. Some more information on this is here: http://developer.yahoo.com/performance/rules.html
s2mu3123
05-29-2009, 12:07 PM
I will suggest you to put either in <head></head> or if it is possible then put at the bottom or where do you want the script to be embedded at your end.
RS Shamil
05-29-2009, 12:20 PM
I'll see if putting them at the bottom helps things :)
EDIT: nope, they all fail -.-, putting them in header makes page load slowly. All js files are compressed and minified.
cygnusd
05-29-2009, 02:19 PM
im not entirely sure, but you should only be calling the included functions inside the onload document event (i.e., when the DOM tree finishes parsing the HTML tree, including all external JS scripts whether in the head or bottom or middle).
Thrivemg
05-29-2009, 05:34 PM
Though i really am just repeating what everyone else is saying above, you should have the script on the bottom and make sure all of your previous tags are closed so that if there is an error in the code the rest of the page can still load.
RS Shamil
05-29-2009, 09:04 PM
Though i really am just repeating what everyone else is saying above, you should have the script on the bottom and make sure all of your previous tags are closed so that if there is an error in the code the rest of the page can still load.
Having them at the bottom breaks the apps which use js though.
dreamrae.com
05-30-2009, 04:27 AM
Having them at the bottom breaks the apps which use js though.
Call the scripts in the correct order and you will be fine. Make sure your element IDs are created before the JS referencing them is loaded. Call the JS scripts in the same fashion ie: don't load dependent JS scripts before independent ones.
Loading the JS files last can speed up page load times. Using a CDN makes the load times even faster.
alfyles
05-30-2009, 03:52 PM
Yep, it'd make a lot faster by using CDN. I've tried it and it did make a whole lot of difference. The thing is most CDNs don't support https/ssl.