views:

52

answers:

4

Hey guys quick question, I am currently echoing a lot of javascript that is based conditionally on login status and other variables. I was wondering if it would be better to simply echo the script include like <script type="text/javascript" src="javascript/openlogin.js"></script> that has been run through a minifying program and been gzipped or to echo the full script in raw format. The latter suggestion is messier to me but it reduces http requests while the latter would probably be smaller but take more cpu? Just wondering what some other people think. Thanks in advance for any advice.

+2  A: 

I would go the first option, even though its an extra request it means the html/php page will be smaller. Also, it is my understanding once the Javascript is cached it won't be requested again whereas the html/php page will be requested every time.

Depending on your javascript functionality you could also add the async="true" to the script include to ensure the page is downloaded first then the javascript.

Castles
thanks for the feedback
Scarface
+2  A: 

Include it externally (your first option). Then when you're doing javascript maintenance, you're not doing it inside PHP as well.

Alex Mcp
yeah good point, what if I have php variables though? Should I just use a php include and put the javascript in a php file?
Scarface
the php variables will also be parsed inside the javascript as long as requested parent file is php. php parent,javascript include, php include.
Codex73
+1  A: 

Including the raw text is preferred if you do not expect the page loads per user to go much beyond 1. If you expect your users to request your page multiple times, then the external, cacheable include is the right option. This is usually the case.

bcherry
thanks, appreciate it
Scarface
A: 

Echo the script include so that the javascript in in an external file and then the browser's cache can do it's job.

xavierm02