Prevent OpenLayers WMS layer cache by browser

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",
    "http://wms.jpl.nasa.gov/wms.cgi",
    {
        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

myWMSLayer.redraw(true);

The parameter true tells the layer to force a redraw.