views:

477

answers:

2

I have a jquery dialog box that opens by means of a link. I would like it so that everytime I open the dialog box the image is refreshed.

I tried something like this:

function open_dialog() {
  $("#imageThumbBox").dialog('destroy');
  $("#imageThumbBox").dialog({
    autoOpen: false,
    closeOnEscape: true,
    resizable: true,
    height: 'auto',
    width: 'auto',
    buttons: {
      Cancel: function() {
       $(this).dialog('destroy');
      }
    },
    close: function() {
      $(this).dialog('destroy');
    },
    open: function() {
    }
  });
  var img = db_cgi_url + "/photo.cgi?do=view;ID=" + ID + ";" + now.getTime();
  $("#my_image").attr("src", img);
  $("#imageThumbBox").dialog('open');
}

I can update any "img" tag fine as long as the div is not located in a dialog box. However, since this one is, the img does not update. The dialog box does not seem 2 be destroyed as it opens up immediatley. I tried updated the image in the open and close functions with no success. Any suggestings?

+3  A: 

Try moving these two lines into the open callback function:

var img = db_cgi_url + "/photo.cgi?do=view;ID=" + ID + ";" + now.getTime();
$("#my_image").attr("src", img);

That will make sure that the element container actually exists.

Also instead of now.getTime() you might try (+new Date()) just for kicks.

Alex Sexton
I tried that with no success. It does not reload the dialog box. It is as if the dialog box cannot be destroyed or reloaded once it is initially loaded.BTW I have "var now = new Date();" at the top of my code
Jonathan
A: 

So I figured out the problem. Ironically it did have to do with the date afterall. I was declaring the variable "now" outside of the function, hence, the date was not actually changing.

so I changed the line to

var img = db_cgi_url + "/photo.cgi?do=view;ID=" + ID + ";" + new Date().getTime();

and voila... no problems!

Jonathan
Doesn't that mean if you did what I suggested, that it would have worked?
Alex Sexton
No it doesn't, because if you actually read my original message than you would see that I already tried to put the two lines in the open and close functions with no success. The problem was not in my placement of of the two lines, but in my placement of the time function.
Jonathan