Why use external javascript?

What are the benefits of using an external javascript file? I just can’t understand this, I see that large sites use them only a few times instead of server ones. Is it just for caching?

If this is a matter of clean code and separation of concerns, then you can still enable it from the server in html. For example, I use SMARTY, and I can just include the file {include file='javascript.js}inside <script></script>. If this is for performance, I see nothing but an additional HTTP request making the external file slower. I'm sure I have to miss something, because all the big sites still do it.

Is it due to file caching? my javascripts are dynamic and should not be cached anyway.

can someone help me 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?

+5
source share
4 answers

Most importantly, the file is cached by the browser. The fewer bytes to send from the server, the better. This is a big part of web performance.

Secondly, it provides modularity.

I'm not sure why your JavaScript is dynamic, but I suggest you rewrite it in a way that removes this need. This in itself may be a problem for you in the future.

+5
source
+5

they also help developers separate the different conceptual areas of their code. This can be a real annoying look at hundreds and thousands of js lines in a single file, on top of complex html.

0
source

In addition to what @Gabriel said, it also helps you use the same function on different pages , with more need for them (.html docs).

0
source

All Articles