[OpenLayers-Users] IMAGE_RELOAD_ATTEMPTS and browser cache

David R Robison drrobison at openroadsconsulting.com
Thu Jul 23 09:48:40 EDT 2009


I have trouble getting OpenLayers to reload failed images. It appears 
that the subsequent attempts still get the image from the browser cache. 
I have modified the onImageLoadError function in Util.js to add a URL 
parameter for force a reload from the server. This seems to fix the 
problem. The new code is shown below. David

/**
 * Function: onImageLoadError
 */
OpenLayers.Util.onImageLoadError = function() {
    this._attempts = (this._attempts) ? (this._attempts + 1) : 1;
    if (this._attempts <= OpenLayers.IMAGE_RELOAD_ATTEMPTS) {
        var urls = this.urls;
        if (urls && urls instanceof Array && urls.length > 1){
            var src = this.src.toString();
            var current_url, k;
            for (k = 0; current_url = urls[k]; k++){
                if(src.indexOf(current_url) != -1){
                    break;
                }
            }
            var guess = Math.floor(urls.length * Math.random());
            var new_url = urls[guess];
            k = 0;
            while(new_url == current_url && k++ < 4){
                guess = Math.floor(urls.length * Math.random());
                new_url = urls[guess];
            }
            this.src = src.replace(current_url, new_url);
        } else {
            this.src = this.src;
        }
        param = this.src.indexOf("&try=");
        if (param > 0) {
            if (this.src.indexOf("&", param) < 0) {
                this.src = this.src.substring(0, param);
            } else {
                this.src = this.src.substring(0, param) + 
this.src.substring(this.src.indexOf("&", param));
            }
        }
        this.src = this.src + "&try=" + this._attempts;
    } else {
        this.style.backgroundColor = OpenLayers.Util.onImageLoadErrorColor;
    }
    this.style.display = "";
};

-- 

David R Robison
Open Roads Consulting, Inc.
103 Watson Road, Chesapeake, VA 23320
phone: (757) 546-3401
e-mail: drrobison at openroadsconsulting.com
web: http://openroadsconsulting.com
blog: http://therobe.blogspot.com
book: http://www.xulonpress.com/book_detail.php?id=2579

 





More information about the Users mailing list