I am using OpenLayers (GeoExt) with google chrome browser. All WMS layer is for live database. I have refreshed the browser at certain interval using javascript but the data is cached in browser and when I refresh web page I see old data. When I clear cache from browser I see new data. How do I prevent caching or clear the cache automatically?

function RefreshPage(Period) {
    setTimeout("location.reload(true);", Period);

2 Answers 2


The trick of using the "myData" parameter should be unnessesarry, as we see in the OpenLayers.Layer.HTTPRequest (which the WMS layer inherits from):

redraw: function(force) { 
    if (force) {
        return this.mergeNewParams({"_olSalt": Math.random()});
    } else {
        return OpenLayers.Layer.prototype.redraw.apply(this, []);

if you do layer.redraw(true); OpenLayers adds a random parameter for you

(besides, setting myData to Math.random() on initialization will not change myData for each update)


If I remember correctly, you can insert your own parameter into the WMS layer configuration:

var wms = new OpenLayers.Layer.WMS("NASA Global Mosaic",
        layers: "modis,global_mosaic",
        transparent: true,
        myData: Math.random()
    }, {
        opacity: 0.5,
        singleTile: true

You can use the current time or another random value. The key myData will be URL encoded into the request sent to the WMS provider and the request is now changed due to the new value of myData.

This doesn't clear the cache, but it tells the browser that the request is different from the one it previously cached.

If you just want to refresh your WMS layers, this can be done by


The parameter true tells the layer to force a redraw.

  • As mentioned by atlefren below, this creates a "static" random number tacked to the request. So each request is still using the same static random number. So adding "myData: Math.random()" will not work. The second option shown allows the user to "redraw" the layer, but when would the user know to do this? I suppose if the data changes frequently, you would simply put this in a timer.
    – Brian B
    May 4, 2015 at 15:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.