Page 1 of 1
site design question on javascript
Posted: Mon Oct 27, 2003 9:27 pm
by m3rajk
as is evident there's a bunch of javscript on my site. right now it's scatttered throught pages. i don't think anything is used more than one place at this point. but since i am adding some more, i was wondering about something. i figured asking those with more experience would be a good thing...
if i move all the javascript to an include file that gets included on all pages, would that have a signifigant affect on load time?
would it be something you would do or consider doing?
or would you only do this if you had functions reused in several pages and then only for that?
thanx in advance for your advice,
-Josh
Posted: Mon Oct 27, 2003 9:44 pm
by JAM
Really depends if I'm asked.
A 'bunch' can be rather much, and a byte is a byte so if you are including a 100 line .js file, yes the load time could differ alot.
This is really up to you alone (benchmarking time?

). I include mine just because it easier to maintain, but I don't think I have nearly as much javascript as you in my work.
Another loose untested idea is the below. Naming functions to match your page-names, and... well, you might get the idea behind it. This is focusing on having all javascript at one place, but only printing it when it is needed...
Code: Select all
// lib_java.php
function javascript_foo() {
// return javascript here
}
function javascript_bar() {
// return javascript here
}
---
// foo.php
if (function_exists('javascript_'.basename(__FILE__,'.php')) {
// run that particular function..
}
Posted: Mon Oct 27, 2003 10:58 pm
by m3rajk
i gathered everything.. i think, incase you'd like an example.. but now that it's gathered, only one non-inine thing would be useful outside of it's original place. so i guess unless i decide to add the character counting elsewhere it's better to keep it as is.
Posted: Tue Oct 28, 2003 12:18 pm
by microthick
Might the .js file not be cached by the browser, reducing load time?
Posted: Tue Oct 28, 2003 8:15 pm
by JAM
Yup, I'm thinking about first impression's counts high. Might not be an issue in this case tho.
I'm also one of alot of people I know of that doesn't use more than 3 days old cache.
Posted: Wed Oct 29, 2003 12:52 pm
by m3rajk
hmm.. i have cache set on "check against server every time"