Changing image src with jQuery. (Doesn't work on Firefox)

前端 未结 1 1921
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-06 23:14

I\'m writing an Ajax contact form. I have written my own captcha too. But I have a problem about refreshing the image. I have written this like that:

Reloading the c

1条回答
  •  不知归路
    2021-01-06 23:55

    It seems that Firefox and IE are caching the image. To prevent this, append a timestamp to the URL and image source:

    In Javascript you can use new Date().getTime():

    $("#captchaSection").load("captcha_p.php?" + new Date().getTime());
    

    In PHP you can use microtime():

    < img src="captcha.php?" name="imgCaptcha" />
    

    I don't see any benefit of using .load() to load HTML that contains the image. It would be easier to just change the src property of the image, for example:

    // refresh captcha
    $('img[name=imgCaptcha]').prop('src', 'captcha.php?' + new Date().getTime());
    

    0 讨论(0)
提交回复
热议问题