简体   繁体   中英

How should I include and reference my JavaScript files

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

Google for their calendar or mail obviously don't download all the JavaScript they need to operate so how do they reference the files they need from the server? Leaving the majority of files on my server would also have the advantage that I would not need to get the user to refresh the browser or log back in again when I update the functionality or fix bugs. how do other people get users to update their web apps?

Also, does anyone have any suggestion on how to secure my scripts?

If you have many script files, but they all contribute to the same functionality then concatenate and compress them into one file.

When you serve your one file help clients know to not cache it (if it changes often) by adding a query string to it (either random or a 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 handle your multiple JS files at build time with a simple shell or batch file. It would concatenating the files then depending on whether you are in RELEASE or DEBUG compress the large file with a JavaScript optimizer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM