tags:

views:

55

answers:

3

What are pros of using an external javascript file? I just can't figure it out, I see big websites using them all around several times instead of server-side includes. Is it just for caching?

If it's a matter of clean code and seperation of concerns, then you can still include it from the serverside into the html. For example I use SMARTY and I can just include the file {include file='javascript.js} inside <script></script> tages. If it's for performance I can't see anything other than an extra http request that makes the external file slower involved. I'm sure I must be missing something because all the big websites still do this.

Is it because of caching the file? my javascripts are dynamic and shouldn't be cached anyway.

could someone help me out to make the right decision to choose what to do with my javascript files.

ps:can a 1.5K user create a tag for external-javascript?

+4  A: 

The most important is that the file is cached by the browser. The fewer bytes that need to be sent from the server the better. This is a big part of web performance.

Second to that, it provides modularity.

I'm not sure why your JavaScript is dynamic, but I suggest you rewrite it in a way that removes that need. That in itself might be an issue for you down the road.

Gabriel McAdams
because I use AJAX techniques and code must differ for every situation.And I don't want to put JavaScript code in-line for everything because of modularity.
Neo
Using AJAX is no reason for dynamic JavaScript. If code must differ for different situations, then you should modify your functions to avoid that. The script should be modular. Think OOP.
Gabriel McAdams
that's exactly why my javascript is dynamic php creates it with the correct syntax based on the page. the reason is complicated as this is part of a migration project from an old framework to a new one.
Neo
If you need different syntax for different pages, then you have an opportunity to rewrite your functions. Either split it into multiple functions or write your function to be more generic / to have more parameters.
Gabriel McAdams
A: 

they also help the developers separate different conceptual areas of their code. It can get real annoying looking at hundred to thousands of lines of js in a single file, on top of complicated html.

hvgotcodes
thats why there is server-side includes.
Neo
@neo that doesn't seem to be what an SSI is for...
hvgotcodes
no I'm not talking about SSI but literally an include on the server side.with php, in my case with SMARTY
Neo
+2  A: 

In your case where there is no caching because the entire javascript file is generated dynamically, inline is probably superior. It saves you the HTTP overhead.

Source: http://developer.yahoo.com/performance/rules.html#external

Adam