For an MVC ASP.NET application, I am adding a javascript file to the _Layout.cshtml file in the following manner –
function loadJS() { var el = document.createElement("script"); el.src = "/Scripts/script.js"; document.body.appendChild(el); } if (window.addEventListener) window.addEventListener("load", loadJS, false); else if (window.attachEvent) window.attachEvent("onload", loadJS); else window.onload = loadJS;
The purpose of this is to only load the script after the DOM has loaded.
Two questions –
- Will
script.jsbe cached by the first page that uses_Layout.cshtml? - If
script.jsis cached, does that mean all the following pages that use_Layout.cshtmlwill not have to retrievescript.jsfrom the server? That the pages that use_Layout.cshtmlwill be able to use the client side cached version ofscript.js?
Thanks!
Yes, when the file is requested, the file will be cached by the browsers. Then the next time the file is requested, the browser will first check its cache and use that one instead of making a request to the server.