Log in

View Full Version : Clear Browser Cache with Javascript?



rapster88
11-15-2007, 08:05 AM
Hi
I'm looking for a script that can clear browser cache at page load.
Or is it possible to clear cache only of images?

tech_support
11-15-2007, 08:50 AM
You can't. The End.

BLiZZaRD
11-15-2007, 08:54 AM
Well.... I wouldn't say the end. it is possible on some degree, but very intrusive and I haven't yet seen a method that works correctly.

You certainly cannot do it with an SSI language or a script. And certainly not without the users knowledge (read: permission).

Perhaps though, we need more information from the O.P. as to a why, perhaps there is something s/he wants done that can be accomplished without clearing cache. Perhaps.

djr33
11-15-2007, 08:55 AM
Security issue and completely impossible, but it might be possible to solve the problem another way, or attach a no-cache header for a page that must reload each time, etc. Once it is on the computer, though, nothing can be done, at all, short of invading the computer with ActiveX or a Java applet, at which point you could just as easily rename their C drive as delete their cache.

rapster88
11-15-2007, 09:04 AM
hmm.. Here's my problem. We update news on our website with own CMS System. Each time the news are updated, images of old news are left in browser cache so viewers need to refresh the browser 2 to 3 times (several sometimes) to get the correct images.

BLiZZaRD
11-15-2007, 09:19 AM
Couple things.

In the <head> section add meta tag:



<meta http-equiv="pragma" content="no-cache" />


Basically the browser won't cache the page.

Another option is to set sessions, this will force the browser to a new session each time they go, and thus make the browser get the page from the server instead of the cache



<?php
session_start();

$_SESSION = array();
session_destroy();

?>

rapster88
11-15-2007, 09:49 AM
I'll try the first one. Can't try second as my page is written with asp.net

djr33
11-15-2007, 10:15 AM
You could also add a random variable to the end of the image:
<img src="image.jpg?state=[random]">
like state=29422488713, then it will think it's a new image each time, and reload it.

jscheuer1
11-15-2007, 02:05 PM
I'll try the first one. Can't try second as my page is written with asp.net

asp.net must have an equivalent to tell the browser not to cache the page, consult the documentation.


You could also add a random variable to the end of the image:
<img src="image.jpg?state=[random]">
like state=29422488713, then it will think it's a new image each time, and reload it.

Random numbers may repeat, it's better to add a number generated from the Date object.

jacobg12
06-30-2011, 07:45 PM
i created an account here only so i could share my dissapointment with this community.


real programmers know nothing is impossible, but rather different approaches may be preferred because they're easier.

this thinking of, 'it isn't possible' is completly ridiculas. i could tell you how it would be possible myself.


the biggest problem: finding out how to compile assembly language inside of javascript. even if you have to dynamically create a text-file, save it as .cpp, and compile it with a mini-compiler; it's possible.

once you have it in assembly.... it's easy. there's not really any such thing as 'security' in the __asm language.


go ahead, argue with me that what's described above is 'impossible' :)


i'll post my code when i have a javascript cookie clearer, if i feel you guys are worthy :) <3

djr33
06-30-2011, 07:55 PM
First, this discussion is three years old. Second, if you are just signing up to complain without any counterexamples then that's not productive. Third, if you do manage to find a complicated way around this, then as soon as you do the browsers will begin to block it as a security risk. This should not be allowed, and if it were it would be dangerous and would need to be blocked. Also, please read an entire discussion before misquoting us. For example, see this post:
Security issue and completely impossible, but it might be possible to solve the problem another way, or attach a no-cache header for a page that must reload each time, etc. Once it is on the computer, though, nothing can be done, at all, short of invading the computer with ActiveX or a Java applet, at which point you could just as easily rename their C drive as delete their cache. Though this is a three-year-old discussion, I don't believe anything has changed.

You can't compile assembly language inside of Javascript. For one thing, Javascript does not "compile"-- it is delivered directly and executed within the browser as code. In that sense it's not the same as other programming languages that are compiled. It's just code-- that's why it's called Javascript.

And again, even if you manage to do this you'll be creating many problems by opening such security risks. We can only hope that the browsers are smart enough to block any potential security risks like that. In fact, I think it's so likely it doesn't seem worth the effort looking for the holes, but go ahead if you'd like.


More generally, this would be possible if it were not blocked. I don't disagree with you. It might even be useful. But it's not allowed, and it shouldn't be because of the potential consequences-- and I personally don't want a website messing with cached files on my computer.
And even if there is a way to do this, then it would either be hacking (and quickly, I hope, blocked by the browsers) or allowed in the security settings. It also would vary by browser and by system, so while it might be possible to do this on some systems/browsers (probably with the user's permission), it would not always work-- what about users on Mac or Linux OS? Or if you did use ActiveX then what about non-IE browsers? Etc.

This doesn't mean there is no solution to the original problem, but it needs to be approached in a different way.



As for the idea of not posting negative comments, I don't agree. If someone is asking a question about something that is not possible, then it is HELPFUL to tell them it is not possible so they don't waste their time. Of course "not possible" must be understood in the context of "within the current status of technology", but effectively, yes, this IS impossible.