Categories: Snippets

Clear the browser cache of CSS or JavaScript Using Javascript

function reloadScripts(toRefreshList/* list of js to be refresh */, key /* change this key every time you want force a refresh */) {
    var scripts = document.getElementsByTagName('script');
    for(var i = 0; i < scripts.length; i++) {
        var aScript = scripts[i];
        for(var j = 0; j < toRefreshList.length; j++) {
            var toRefresh = toRefreshList[j];
            if(aScript.src && (aScript.src.indexOf(toRefresh) > -1)) {
                new_src = aScript.src.replace(toRefresh, toRefresh + '?k=' + key);
                // console.log('Force refresh on cached script files. From: ' + aScript.src + ' to ' + new_src)
                aScript.src = new_src;
            }
        }
    }
}

 

Share

Recent Posts

Selecting Elements in jQuery (jQuery Selectors)

To collect a group of elements, we pass the selector to the jQuery function using…

6 years ago

jQuery library Introduction

jQuery is a lightweight, "write less, do more", JavaScript library. The purpose of jQuery is…

6 years ago

Adjust li width fit with ul width of the outer div/body

HTML:- [crayon-67a64e1497dba741417909/] CSS:- [crayon-67a64e1497dbf974209183/] JavaScript:- [crayon-67a64e1497dc0673877039/]  

6 years ago

Customize Input Box using jQuery

Prevent users typing special characters in text box, textarea, etc. [crayon-67a64e1497f55347138448/] ^[a-zA-Z]+$ Alphabets ^[a-zA-Z\s]+$ Alphabets…

6 years ago

JavaScript simple form validation with example

Any interactive web site has form input - a place where the users input different…

6 years ago

Simple Ajax Call

[crayon-67a64e14983a0862494360/]  

6 years ago