Good people in the html5 template recommend putting all your javascript in script.js so that the browser only has to download one file (along with the others that h5bp uses) and enable caching of this file.
The idea is not to get into the โrecommendedโ way, but to think about things related to your own applications.
- This geolocation file will only be used on this page, right? It will never be used anywhere else.
- The script.js file will be used on several pages.
Well, then it would be pointless to put the "whole script" that will be needed on only one page in the script.js file. You must make the file external and call it separately on the desired page. This will not allow you to inflate the script.js file for functionality that this user can never use.
However, if your "whole script" for the geolocation function is quite small, include it in script.js. If it does not add download speed for this file, then it makes sense to include it there.
The bottom line is: What is the best compromise for my application?
We know that this is true:
- Cached js files are good
- fewer files to upload well
- smaller files to upload are good
- important service
Once you think about these things from the point of view of your application, making decisions becomes a little easier. And remember, solutions that trade in milliseconds will not matter much to your perception by the user of how fast your page is.
source share