最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

why use external javascript? - Stack Overflow

programmeradmin0浏览0评论

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?

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?

Share Improve this question asked Oct 13, 2010 at 18:18 NeoNeo 11.6k3 gold badges71 silver badges81 bronze badges 0
Add a ment  | 

4 Answers 4

Reset to default 5

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.

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./performance/rules.html#external

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 plicated html.

Besides what @Gabriel said, it also helps you use the same function in different pages, withouth the need for them (.html docs) to be larger.

发布评论

评论列表(0)

  1. 暂无评论