0

I need to load and display an image (so far from folder) on a click of the button (without providing user with the opportunity to choose/change it).

The problem: although filename will stay the same, the image file itself might change after the page is loaded (it keeps being updated).

So pre-defining the source like this <img id = "bla" src="bla.png" align="right"> wouldn't work. What would be a normal way of loading an image in a sort of dynamic fashion from scratch when .html has loaded?

3
  • I'm not positive, but if the image is sent with a no-cache header, then resetting the src to the image's URL might do the trick. Commented Sep 3, 2019 at 8:48
  • if you are looking to load an image from the user's computer without user interaction I think you are out of luck Commented Sep 3, 2019 at 8:51
  • lucas, eventually it will be on company server but at this point I need it to work at least on my machine with localhost. Chris G how do I set no-cache header? Commented Sep 3, 2019 at 8:54

3 Answers 3

3

You can add timestamp to make a different GET request each time.

Try something like url + "?" + new Date()

Sign up to request clarification or add additional context in comments.

Comments

0

you can implement an onclick handler to dynamically load and display the image once the button is clicked. that is in contrast to preload the image and not display it until the button is clicked (clicking the button unhide the image, while the image can be changed from the time it was read until the time it was displayed).

once the image is displayed, you can periodically reload (and display) the image, but this approach of polling is costly since operations is taking place when the image might not have been changed.

rather than using polling, you can reload (and display) the image on-demand when the server notifies the client that the image has been updated. you can achieve it using push api (but this is just an example)

Comments

0
style {
 #imageToDisplay{
     display:none;
  }
 .show{
   display:block;
 }
}

<div id="imageToDisplay">
   <img id = "bla" src="/fromFolder/bla.png" align="right">
   <button id="showImage">Click to Show</button>
</div>
<script>
    $("body").on("click","#showImage", function(){
       $("$imageToDisplay").toggleClass("show");
    });
</script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.