Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reload image without refreshing the page

Javascript:

function atualiza() {
  document.getElementById('badge').innerHTML = location.reload();
}

I know the "location.reload()" will refresh the page...

HTML:

<img id="badge" src="<?php echo $cms_url; ?>/imaging/badge.php?badge=$mygroup['badge']; ?>" />

I need to freshed the src="" or <img /> without refreshing the page.

like image 263
jcsantos Avatar asked Jan 24 '14 01:01

jcsantos


2 Answers

See this: Updating a picture without page reload

document.getElementById('yourimage').src = "url/of/image.jpg?random="+new Date().getTime();

anyways, this was copied off that thread and apparently it should reload the image.

like image 147
Kevin Avatar answered Sep 27 '22 21:09

Kevin


Maybe the javascript i wrote is usefull for someone who reads this question. it ads a timestamp to every image to break the browser cache:

<script type="text/javascript">  

    function replaceSrc()
    {

        var images = document.getElementsByTagName('img');

        for(var i = 0; i < images.length; i++)
        {
            var dt = new Date();
            var img = images[i];

            if(img.src.length >= 0 & img.id != 'idImageNoTimestamp')
            {
                img.src = img.src + "?" + dt.getTime();
                //javascript:alert(document.lastModified);
            }
        }
    }
    replaceSrc();
      </script>

you can exclude some images by using the id attribute. I used it to exclude Google static maps images. if you don't need it, remove:

 & img.id != 'idImageNoTimestamp'
like image 31
Aurora Avatar answered Sep 27 '22 22:09

Aurora