Use AJAX to reload captcha

旧街凉风 提交于 2019-12-03 20:53:40

Unfortunately, AJAX doesn't provide a good way to dynamically load images. Even using the javascript "preload" trick just gets the browser to load each image once per URL. The only good way to get the browser to load another image from the same source is to use a different parameter just like you are doing now. And, like other answers have stated, timestamp should be sufficient for that.

Have you considered using a timestamp instead?

onclick="this.src='captcha.php?ts='+Math.round(new Date().getTime()/1000)"

Just use:

<img src="captcha.php" onclick='this.src = "captcha.php&time=" + new Date().getTime();' />

You can discard the time parameter and generate the random number in PHP. :)

You could also get the image from an Ajax request base64 encoded and put it into the img tag too.

Of course I think it is overkill and a base64 encoded file is about 4/3 of the original's size. (A 3 kb image would be about 4kb on base64).

Edit: to have the img src attribute like

data:image/png;base64,base64encodedimage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!