Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Aug 2002
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Odd caching and cookies in Opera

    I'm having some serious problems getting Opera not to cache a javascript file. I have this code on a page:

    <SCRIPT LANGUAGE="JavaScript" SRC="http://192.168.0.101/code.js">
    </SCRIPT>

    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?

  • #2
    Senior Coder
    Join Date
    Jun 2002
    Location
    near Oswestry
    Posts
    4,508
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #3
    New to the CF scene
    Join Date
    Aug 2002
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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));
    }

  • #4
    New to the CF scene
    Join Date
    Aug 2002
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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!

  • #5
    Senior Coder
    Join Date
    Jun 2002
    Location
    near Oswestry
    Posts
    4,508
    Thanks
    0
    Thanked 0 Times in 0 Posts
    No worries; Opera's caching behaviour is very annoying ...


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •