Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically change image src using Jquery not working in IE and firefox

I am implementing a captcha for a email. when click on linkEmail button email modal will open. there i have to set captcha image generated by a handler (CaptchaGenerator.ashx) on click of linkEmail button click. Here is the code for that.

$(".linkEmail").click(function () {
  //Load captcha image
  $('.imgCaptcha').attr('src', '/Custom/AppCode/Utilities/CaptchaGenerator.ashx');
  $('#emailModal').modal();
});

Above code is working fine in crome but not working in IE and firefox. Although i have tried followings there is no luck.

HTML:

<p id="captchacontainerp" class="captchacontainer"></p>
-------------------------------------------------------------
$('#captchacontainerp').prepend($("<img id='imCaptcha' class='imgCaptcha' src='/Custom/AppCode/Utilities/CaptchaGenerator.ashx'></img>"));
-------------------------------------------------------------
var img = $('<img id="imCaptcha" class="imgCaptcha">');
img.attr('src', '/Custom/AppCode/Utilities/CaptchaGenerator.ashx');
$('#captchacontainerp').empty();
img.appendTo('#captchacontainerp');
---------------------------------------------------------------
$('#captchacontainerp').empty();
$('#captchacontainerp').append($("<img id='imCaptcha' class='imgCaptcha' src='/Custom/AppCode/Utilities/CaptchaGenerator.ashx'></img>"));
like image 829
Madurika Welivita Avatar asked Jan 04 '13 10:01

Madurika Welivita


2 Answers

IE caching all GET request, so add a timestamp to your request URL e.g :

$(".linkEmail").click(function () {
   //Load captcha image
   $('.imgCaptcha').attr('src', '/Custom/AppCode/Utilities/CaptchaGenerator.ashx?'+new Date().getTime());
   $('#emailModal').modal();
});
like image 78
A. Wolff Avatar answered Nov 04 '22 19:11

A. Wolff


Have you tried setting the src attribute to '' before changing it again? Also, what are the caching settings you are using (both locally, and on the server)

like image 1
Kippie Avatar answered Nov 04 '22 19:11

Kippie