Is it possible?
While it is best to have only one Javascript file per page to reduce the number of requests, this may not be possible. Especially how you would like to do this.
If you ask how to join different scripts of different pages in one script, and then run only those parts that are associated with a specific page, then this is what you should not do. What good is it for you to have one huge file with a lot of scripts (also think about maintainability) compared to a few short integrated scripts? If you keep the number of scenarios low (i.e. below 10), you should not worry.
The big disadvantage is that the browser will download the full script file, which means that it will take more time to analyze them, and also use a lot more resources to use it. I would highly recommend against your technique , although this may seem interesting ...
Other features
The fact is that the number of Javascript files on the page is small. Depending on the technology you are using, there are tools on the server side that can combine several script files into one, so that each page will only request one script file that will combine all the scripts that this particular page will use. There is a bit of overhead on the server to complete this task, but there will be only one script request.
What are you getting?
- On each page there are only scripts that he needs.
- individual script files are smaller, so itβs easier to maintain
- script request size is small
- Browser parsing and resource consumption remain low.
source share