Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Any disadvantages to Javascript inside the page?

In my build (for a complex webapp), I've aggregated all the javascript into 1 file, which I'm loading as script.js

I thought I might go even further and just print all the js into html. Are there any reasons I should not do that? My thinking is... why not just save the request?

The only downsides I'm aware of are: I understand that since the js is pretty huge, the initial page load might get slowed down.

I'm not very concerned about that since the page is empty anyway without javascript.

Also the script.js could be cached. If I wanted to cache the script within the html, I would have to use varnish or the like.

What are some reasons why I should not do this? Thanks.

Edit: I forgot to mention that this is a 1 page javascript app, so every single page has the same javascript (and html).

like image 766
Harry Avatar asked Dec 17 '22 16:12

Harry


1 Answers

I use one single minified external js file. As a seperate file it can be cached nicely, although an extra HTTP request is needed.

If you put your java script inline then it wont be cached as simply and you will have duplication of code on every page if you are reusing methods that are inline.

like image 169
Andrew Avatar answered Dec 30 '22 23:12

Andrew