How to load Javascript Files in client system once and reuse them?
I have a large scale application which uses javascript for many reasons. these javascript files included in some JS files and JS files imported to Asp.net Masterpage files.
this JS files contents (javascript functions) will add to page when page display to user, so a Bunch of waste Javascript functions will include in Asp.net pages.
I'm lookingfor a way which these JS files downloads to users computer (any where,开发者_开发百科 e.g: temporary internet files or some where else) and the Asp.net page use that functions which downloaded with page.
I also want to download 1 time ( per user login or per Open Session ) to users computer.
So How to download JS file to Users PC and Use them in Asp.net Pages
What the commenters have suggested is that all the javascript should be placed on external files. According to my understanding what you are doing is:
<script language="javascript">
your code here
</script>
Instead you should have.
<script src="myjavascript.js" type="text/javascript"></script>
Then your javascript files will be automatically cached by the browser.
MindFold also goes on to suggest using google's CDN . which is a good suggestion. Of course you can only use it for popular libraris like Jquery.
Check out ASP.NET Performance - Part 2 - YSlow by Karl Seguin on how to write a HttpHandler to add caching headers for static files.
I would advise you to read the other 4 posts as well on caching strategies for ASP.Net. This will give you a better understanding on what's going on when serving pages from a webserver to a browser.
Client side caching is, well stating the obvious, dependent on the clients behavior. Have a look at http://code.google.com/speed/page-speed/docs/caching.html. Some of the recommendations are
- Set
Expires
instead ofCache-Control
(as it is more widely supported by clients). - Avoid setting the
Vary
header (IE). - When fetching files over https use
Cache control: public
(Some versions of FF)
There are also some suggestions about how to allow web-proxies to cache (and funnilly enough the recommendation: Don't include a query string in the URL for static resources., is broken by their own CDN)
The browser does exactly that automatically for you, whenever a browser downloads a file(in your case a js) he stores it in a temp directory for later usage thus optimizting overhead, the same applies for pictures.
You can farther optimize it by using some sort of CDN, for example Google have a cdn for many common JavaScript libraries.
So, once again - after the users browser downloaded the script, he will try to re-use it, based on name.
so, if your scripts resides on the same place, you get this functionality for free!
精彩评论