5 ms·
Not a Web guy here: why do people use <script src="foo.js" /> instead of just including the code inline in the script tag? I understand that as a dev you defin
by bjackman 13y ago
Not a Web guy here: why do people use <script src="foo.js" /> instead of just including the code inline in the script tag?
I understand that as a dev you definitely want your JS in separate files for practical reasons, but couldn't you just have separate .js files and have your framework stick them inline "at runtime"?
PS: Is there any talk with HTTP 2.0 of allowing data like images to be somehow "inlined"? It would then be possible to serve up a 21st century website with a single RTT, right
- tonyblundell 13y agoIf the code is inside the script tag, it has to be downloaded with each page. If it's a separate file, it only has to be downloaded once, when the first page is loaded.
- nolok 13y agoIf I use foo.js, when the user reload the page, or goes to another page that also uses foo.js, the javascript file is in his cache and he won't need to re-download it. Given the size some of those js are, it makes a significant decrease. Especially in the mobile context this article talks about, when I visit my parents my phone's data connection is slow enough that 40kb of js make a huge difference. Of course, that also needs a properly configured server (a very large number of sites out there don't see proper client caching so the browser still need to ask the server if the file has changed every single time, for example).
- dpweb 13y agoNot in-lining the JS keeps a nice separation between your html and js files, but I can see either approach. You can inline imgs now with Data URI. The real issue is design decisions, jq's not incredibly light, but at least there is some real value in it. Those numbers on avg web page size and rts are atrocious, people should be ashamed of themselves.
- albemuth 13y agoDoing that would be adding filesize to the request, so the amount of time to show "something" would be increased. It would also make it harder to cache your scripts (maybe using session + localstorage :/). Can't comment on http 2.0.