How to programmatically empty browser cache?

前端 未结 11 859
迷失自我
迷失自我 2020-11-22 17:36

I am looking for a way to programmatically empty the browser cache. I am doing this because the application caches confidential data and I\'d like to remove those when you p

11条回答
  •  清酒与你
    2020-11-22 17:43

    //The code below should be put in the "js" folder with the name "clear-browser-cache.js"
    
    (function () {
        var process_scripts = false;
        var rep = /.*\?.*/,
        links = document.getElementsByTagName('link'),
        scripts = document.getElementsByTagName('script');
        var value = document.getElementsByName('clear-browser-cache');
        for (var i = 0; i < value.length; i++) {
            var val = value[i],
                outerHTML = val.outerHTML;
            var check = /.*value="true".*/;
            if (check.test(outerHTML)) {
                process_scripts = true;
            }
        }
        for (var i = 0; i < links.length; i++) {
            var link = links[i],
            href = link.href;
            if (rep.test(href)) {
                link.href = href + '&' + Date.now();
            }
            else {
                link.href = href + '?' + Date.now();
            }
        }
        if (process_scripts) {
            for (var i = 0; i < scripts.length; i++) {
                var script = scripts[i],
                src = script.src;
                if (src !== "") {
                    if (rep.test(src)) {
                        script.src = src + '&' + Date.now();
                    }
                    else {
                        script.src = src + '?' + Date.now();
                    }
                }
            }
        }
    })();
    At the end of the tah head, place the line at the code below
    
        < script name="clear-browser-cache" src='js/clear-browser-cache.js' value="true" >< /script >

提交回复
热议问题