Home  >  Q&A  >  body text

How to clear browser cache programmatically?

<p>I'm looking for a way to clear the browser cache programmatically. I'm doing this because the app caches confidential data and I want to delete those when you press "logout". This can be achieved via the server or JavaScript. Of course, using this software on foreign/public computers is still discouraged as there are more dangers like keyloggers that you just can't beat on a software level. </p>
P粉419164700P粉419164700445 days ago653

reply all(2)I'll reply

  • P粉990568283

    P粉9905682832023-08-02 11:31:57

    It is impossible for the browser to let you clear its cache. If this were possible, this would be a huge security issue. This is easily abused - as soon as a browser supports this "feature" I will uninstall it from my computer.

    What you can do is tell it not to cache your page, by sending an appropriate header or using these meta tags:

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

    You may also want to consider turning off autocomplete on form fields, although I'm afraid there is a standard way to do this (see this question).

    Anyway, I would like to point out that if you are dealing with sensitive data, you should use SSL. If you don't use SSL, anyone with access to the network can sniff network traffic and easily see what your users see.

    Using SSL also enables some browsers not to use caching unless explicitly told to do so. Take a look at this question.

    reply
    0
  • P粉680487967

    P粉6804879672023-08-02 09:30:01

    It is possible, you can simply use jQuery to replace the "meta tag" referencing the cached state with an event handler/button and then refresh, easily.

    $('.button').click(function() {
        $.ajax({
            url: "",
            context: document.body,
            success: function(s,x){
    
                $('html[manifest=saveappoffline.appcache]').attr('content', '');
                    $(this).html(s);
            }
        }); 
    });

    Note: This solution relies on the application cache implemented as part of the HTML 5 specification, it also requires server configuration to set up the application cache manifest. It doesn't describe a way to clear "traditional" browser cache via client-side or server-side code, which would be nearly impossible to do.

    reply
    0
  • Cancelreply