Force Remote Image to refresh

You must Login before you can answer or comment on any questions.

I am currently loading a remote image (news.jpg) so that I can change it with the latest news but once it loads, it is not updating - does anyone know of a way to force it to refresh or to clear the image cache?

var win = Titanium.UI.currentWindow;
 
var tableView;
var data = [];
 
{
    var row = Ti.UI.createTableViewRow();
    row.width = 'auto';
    row.height = '200'
    row.hasChild = false;
 
    var photo = Ti.UI.createImageView({
        image:'http://www.URL.org/mobile/images/news.jpg',
        top:0,
        left:0,
        bottom:0,
        width:'auto',
        height:'auto',
    });
    row.add(photo);
 
    data.push(row);
}
 
{
    var row = Ti.UI.createTableViewRow();
    row.selectedBackgroundColor = '#fff';
    row.height = 'auto';
    row.hasChild = true;
    row.test = 'newsrss.js';
 
    var photo = Ti.UI.createView({
        backgroundImage:'../images/icon.png',
        top:35,
        left:10,
        width:74,
        height:60,
    });
    row.add(photo);
 
    var user = Ti.UI.createLabel({
        color:'#000000',
        font:{fontSize:22,fontWeight:'bold'},
        left:100,
        top:10,
        height:30,
        width:200,
        text:'News'
    });
 
    row.add(user);
 
    var comment = Ti.UI.createLabel({
        color:'#222',
        font:{fontSize:16,fontWeight:'normal'},
        left:100,
        top:38,
        bottom:17,
        height:60,
        width:200,
        text:'Stay up to date with the latest news.'
    });
    row.add(comment);
 
    data.push(row);
}
 
tableView = Titanium.UI.createTableView({
    data:data,
    scrollable:true,
    backgroundColor:'white'
});
 
tableView.addEventListener('click', function(e)
{
    if (e.rowData.test)
    {
        var win = Titanium.UI.createWindow({
            url:e.rowData.test,
            title:e.rowData.title
        });
        Titanium.UI.currentTab.open(win,{animated:true});
    }
});
 
win.add(tableView);

— asked 10 months ago by J W
0 Comments

1 Answer

Accepted Answer

Try appending a param to your request. In this case, the milliseconds from the current time (0-999)

var d = new Date();

image:'http://www.URL.org/mobile/images/news.jpg?'+d.getMilliseconds(),

Or d could be a random number:

var d = Math.floor((Math.random()*100)+1);

— answered 10 months ago by Stephen Feather
answer permalink
5 Comments
  • Stephens answer is a great and simple trick I used for years.

    — commented 10 months ago by Malcolm Hollingsworth

  • Malcolm, thank goodness web servers are kind of dumb.. grin

    — commented 10 months ago by Stephen Feather

  • so who wants to explain to me why appending a number to a image file will force it to refresh?

    — commented 10 months ago by Jin An

  • Show 2 more comments

Your Answer

Think you can help? Login to answer this question!