NameSilo

No Cached Image

Spaceship Spaceship
Watch

newsiness

VIP Member
Impact
49
Everything works fine except that when a new image is uploaded to replace an existing one, the browser still displays the former one until i refresh the page.

By using this piece of PHP, the browser thinks its a new image and reloads.
Code:
<img src="img.jpg?rand=<?=rand(1,1000);?>" />

Anyway to complete the same task in Javascript instead of PHP?

Thanks in advance!!!
 
Last edited:
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
GoDaddyGoDaddy
Code:
<script language="javascript">
document.write('<img src="img.jpg' + Math.ceil(Math.random() * 10000) + '" />');
</script>
Something like that.
 
1
•••
Its based on Eric's explanation :tu:

Code:
<script type="text/javascript">
	function refreshImage(imageId) {
		var objImage = document.getElementById(imageId);
		var oldSrc = objImage.src;
		var cleanSrc = oldSrc.substr(0, oldSrc.indexOf('?'));
		var newSrc = cleanSrc + "?" + Math.ceil(Math.random() * 10000);
		//alert(newSrc);
		objImage.src = newSrc;
	}
</script>


<!-- Put at least one ? here -->
<img src="yours.jpg?" id="your-image" />

<input type="button" value="refresh" onclick="refreshImage('your-image')" />
 
1
•••
Thanks Guys!!!
Rep Added. :)
 
0
•••
Appraise.net

We're social

Unstoppable Domains
Domain Recover
DomainEasy — Live Options
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back