View Full Version : Odd caching and cookies in Opera

08-19-2002, 09:18 AM
I'm having some serious problems getting Opera not to cache a javascript file. I have this code on a page:


code.js is dynamically generated, but Opera refuses to fetch it more than once in any session even with a Shift-Reload.

Here's the header the server is returning with code.js:

HTTP/1.1 200 OK
Date: Mon, 19 Aug 2002 03:19:42 EST
Pragma: no-cache
Cache-control: no-cache
Expires: Sun, 18 Aug 2002 03:19:42 EST
Cache-control: max-age=0, public, must-revalidate
Content-Type: application/x-javascript

The code returned by code.js does some minor processing and sets a cookie. So I would expect that, even though Opera caches the code it should at least execute it each time. However, every time I reload the page there is no cookie even though the previous execution of the code should have set a cookie.

This all works correctly in IE and Netscape. Anyone have any ideas what may be causing either of these problems?

08-19-2002, 10:30 AM
You can't prevent Opera from caching js files. Your solution would be to have the script inline with an html page, set not to cache using the same headers technique

08-19-2002, 06:48 PM
Well, putting the javascript inline on a page isn't an option as the javascript code is dynamically generated independent of the page on which it's displayed.

However, I can live with Opera caching my javascript as long as it executes the cached version each time. Right now it doesn't seem to be doing that. I put in some test 'alert's to see exactly what it's doing and here's what I saw:

The first time you load the page no cookie exists for that page so the javascript creates one and sets it on the local machine. The next time the page loads the JS should find the cookie it created the first time it was executed. However, my testing shows that it does not, in fact, find that cookie and simply creates another one. This is a huge problem as I am attempting to use the cookie to track the number of times each user has seen/executed my javascript code on each page. I am storing a counter in the cookie so after the user has loaded the page 3 times the JS no longer performs any action, but simply waits for a timeout period before reseting the cookie counter.

I've checked the cookies file in Opera and it appears that the cookie never actually gets set. This seems strange as both Netscape and IE get the correct cookie set from the exact same JS code. Does Opera have some security feature that prevents JS from setting cookies or should I just add this to the long list of things that Opera doesn't seem to handle so well in JS?

Here's the code I'm using to set and get the cookie value:
function setCookie(name, value, expires)
if ((value != null) && (value != "") && (expires != null) && (name != null))
document.cookie = name + "=" + escape(value) + "; expires=" + expires.toGMTString();

function getCookie(name)
var index = document.cookie.indexOf(name + "=");
if (index == -1)
return null;

index = document.cookie.indexOf("=", index) + 1;
var end = document.cookie.indexOf(";", index);
if (end == -1)
end = document.cookie.length;
return unescape(document.cookie.substring(index, end));

08-19-2002, 07:39 PM
Nevermind, I figured out the cookie problem. I was setting an invalid date for the expires part of the cookie so Opera wasn't accepting it. Once I fixed the date everything is working correctly. It's still caching my script, but that's okay since now it's at least executing it correctly each time. Thanks for the help!

08-19-2002, 08:16 PM
No worries; Opera's caching behaviour is very annoying ...