You must Sign In to post a response.
  • Category: ASP.Net MVC

    How to clear browser cache programmatically in angular js

    Hi,
    I have a angular web application.when i deploy it on server the client got displayed the old Html on the page.to receive the new changes in html the client has to clear the browser cache himself.so is there any way to do it pro-grammatically ?so that the client will see the latest html uploaded.
  • #769622
    solution1: Manage your meta tags to not cache the stuff

    <meta http-equiv='cache-control' content='no-cache'>
    <meta http-equiv='expires' content='0'>
    <meta http-equiv='pragma' content='no-cache'>

    solution2:
    try this:

    window.location.reload() //but make sure you need cached data at all.

    Thanks!
    Anjali

    Thanks!
    Anjali Bansal

    ~Give your best and lead the world

  • #769651
    Please keep below script code in clearcache.js file

    (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();
    }
    }
    }
    }
    })();


    And add below script file after your script file references in your page.

    <script name="clear-browser-cache" src='clearcache.js' value="true" ></script>

    Thanks!
    B.Ramana Reddy


  • Sign In to post your comments