When changing JavaScript (in a separate file) and when the page is reloaded, the old script is loaded from the browser cache. Reloading the page with Ctrl-F5 loads the new script. Is there any command that on the server side tells the browser to download non-cache JavaScript?

For example, so that the browser does not cache the page I use

protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) Response.Cache.SetCacheability(HttpCacheability.NoCache); ... } 

You need the same thing, but about the scripts in separate files!

UPD: For critics: It is necessary to remove caching only for convenience of testing, after starting the application I will remove these lines.

    3 answers 3

    Found an interesting approach in the network:

    Quote:

    You can pass the URL of your script tag. This will not be the case.

    If you want to do it, you should not be able to make it.

     <script src="/script.js?time_stamp=1224147832156" type="text/javascript"></script> <script src="/script.js?svn_version=678" type="text/javascript"></script> 

    A source:

    https://stackoverflow.com/questions/206783/when-does-browser-automatically-clear-javascript-cache

    UPD: for completeness

      <script src="/script.js?timestamp=<%= DateTime.Now.ToString("yyyyMMddHHmmss") %>" type="text/javascript"></script> 
    • This is quite a popular approach =) - Aleksey Sonkin
    • I did not know)) - Georgy

    I do exactly that. I add timestamp to the link to the script.

     //функция загружает яваскрипт function get_js(sname) { var loadedJS = document.createElement('script'); //создаем DOM-элемент и задаем его параметры loadedJS.src = sname; loadedJS.type = "text/javascript"; loadedJS.language = "javascript"; var head = document.getElementsByTagName('head')[0]; //получаем элемент секции head head.appendChild(loadedJS); //вставляем новый script в head } function get_timestamp() { //возвращает таймстемп var obj_date = new Date(); var timestamp = obj_date.getTime(); return timestamp; } //вызов функции подгрузки get_js("modules/translation/mech_vars.js?randomData="+get_timestamp()); 

      This is because scripts are not handled by ASP.NET. You must correctly configure the IIS server. Or use the file renameadd request to the name of the js file as a workaround.