Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cons of external JavaScript file over inline JavaScript

What are some of the disadvantages of using an external JS file over including the JS as a part of the ASPX page?

I need to make an architectural decision and heard from coworkers that external JS does not play nice sometimes.

like image 512
DotnetDude Avatar asked Feb 25 '09 16:02

DotnetDude


People also ask

What are the advantages of using external JavaScript over internal and inline?

External JavaScript AdvantagesIt separates HTML and code. It makes HTML and JavaScript easier to read and maintain.

What is the benefits of using scripts in an external file rather than inline?

External scripts Gives better separation of concerns and maintainability. The async and defer attributes have effect so if this attributes are present the script will change the default behavior. This is not possible with inline scripts.

When would it be a good idea to use an external JavaScript file?

To enhance performance, try to keep JavaScript external. The separate code makes it easier for web browsers to cache. However, use inline scripts only when you're making single page websites. Still, it's better to use external code i.e. external JavaScript.

What is the difference between internal JavaScript and external JavaScript?

Internal JavaScript : JavaScript code is placed in the head and body section of an HTML page. Output : This is Internal Javascript Example.!!! External Javascript : JavaScript code are stored in separate external file using the .


2 Answers

The only downside that I am aware of is the extra HTTP request needed. That downside goes away as soon as the Javascript is used by two pages or the page is reloaded by the same user.

like image 177
andynormancx Avatar answered Sep 23 '22 12:09

andynormancx


One con is that the browser can't cache the JS if it's in the page. If you reference it externally the browser will cache that file and not re-download it every time you hit a page. With it embedded it'll just add to the file-size of every page.

Also maintainability is something to keep in mind. If it's common JS it'll be a bit more of a pain to make a change when you need to update X number of HTML files' script blocks instead of one JS file.

Personally I've never run into an issue with external files vs embedded. The only time I have JS in the HTML itself is when I have something to bind on document load specifically for that page.

like image 42
Parrots Avatar answered Sep 24 '22 12:09

Parrots