admin管理员组文章数量:1327683
How can I reload this image to get the new one from the server, using javascript without needing to reload the page?
<img src="?option=_fcse&task=getimage&width=100&height=40&characters=5&r=<?php echo mt_rand(0, 10000000); ?>" />
Thank you
How can I reload this image to get the new one from the server, using javascript without needing to reload the page?
<img src="?option=_fcse&task=getimage&width=100&height=40&characters=5&r=<?php echo mt_rand(0, 10000000); ?>" />
Thank you
Share edited May 2, 2013 at 8:20 themhz asked May 2, 2013 at 8:03 themhzthemhz 8,42422 gold badges85 silver badges109 bronze badges 2-
2
Have you considered Googling
javascript reload image
? – Pekka Commented May 2, 2013 at 8:03 - Yes but those examples look like they are cashing my image. probably I need to reset the rand in javascript now – themhz Commented May 2, 2013 at 8:07
3 Answers
Reset to default 2This should do it:
thatImgObj.src = thatImgObj.src.replace(/&r=\d+/, "&r="+ ~~(Math.random()*1e7));
(Of course, with a chance of 1e-7 to getting the same cachebreaker again - a timestamp might be a better option)
Well the simplest way I think would be changing the src
attribute in Javascript. So like: YouImageElement.src += "&rand2"+Math.random();
Modifying the src
attribute would cause the reload of the image and because it has another random number set Client Side, it shouldn't be cached.
Your code
Added id="img1"
<img id="img1" src="?option=_fcse&task=getimage&width=100&height=40&characters=5&r=<?php echo mt_rand(0, 10000000); ?>" />
JS Code:
var newImage = new Image();
newImage.src = document.getElementById("img1").src + "&_=" + new Date().getTime();
Added a cachebreaker at the end of the url
本文标签: jqueryHow to reload image using javascriptStack Overflow
版权声明:本文标题:jquery - How to reload image using javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742221231a2435454.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论