How should I include and link to my JavaScript files

I currently include about 8 JavaScript files with various classes and functions inside them. I cannot help but feel that I am doing all this wrong. Should I only include some basic thin client-side JavaScript, and then get the functions in other ad-hoc files? Can I reference the JavaScript code on my server from the JavaScript code currently in the user's browser?

Does Google explicitly not load all the JavaScript they need for their calendar or mail, since they link to the files they need from the server? Exiting most of the files on my server will also have the advantage of not having to force the user to refresh the browser or log back in when I update functionality or fix errors. how do other people get users to update their web applications?

Also, does anyone have any suggestions on how to protect my scripts?

+4
source share
1 answer

If you have many script files, but all of them contribute to the same functionality, then they are combined and compressed into one file.

When you serve your file, client clients know that they do not cache it (if it changes frequently), adding a query string to it (arbitrary or version of the file).

<script type="text/javascript" src="file.js?<%=DateTime.Now.Ticks.ToString() %>"></script> <script type="text/javascript" src="file.js?<%=Globals.JS_COMPILED_VERSION %>"></script> 

You can process multiple JS files during build with a simple batch or batch file. It will concatenate files, then depending on whether you are in RELEASE or DEBUG, compress a large file using the JavaScript optimizer.

+1
source

Source: https://habr.com/ru/post/1400012/


All Articles