0

Is that a way to do it better? [JS]

Hello, I'm not an advanced programmer, and I have created a simple dynamically changing information display. <div id="oculus"> <p id="default">Click the position on the list to display informations!</p> <a href="" target="blank_" id="link">Wiki</a> <img src="" id="image" align="center"> </div> And JS is changing innerHTML, href and src of these using onclick event. The problem is, that images are loading slowly. Is there a way to load images before displaying f.e. using onload event?

11th Apr 2017, 8:56 AM
Kuba Nawieśniak
Kuba Nawieśniak - avatar
6 Respuestas
+ 1
// you can load image into cache on window onload var img = new Image(); img.src = "http://path/img.jpg"; // set it to img src later whenever needed x = document.getElementById("later"); x.src = img.src;
11th Apr 2017, 10:13 AM
Calviղ
Calviղ - avatar
11th Apr 2017, 11:36 AM
Calviղ
Calviղ - avatar
+ 1
Try to compare the cache image with normal load image from this link: https://code.sololearn.com/WayQI3dBpmtA/?ref=app The image is HD image, you can wait for the cache image loaded (it would takes 10-20 seconds to load, depends on the internet speed) and trigger the button to see the instant large image loading from cache.
11th Apr 2017, 3:13 PM
Calviղ
Calviղ - avatar
0
Oh, and I don't want to do different <img> tags and display: none; these, because that would have been hilarious .-.
11th Apr 2017, 9:05 AM
Kuba Nawieśniak
Kuba Nawieśniak - avatar
0
No, no, I'm looking for a way to load an image on page load when it's not actually on page, not displaying it only after loading ^^
11th Apr 2017, 9:46 AM
Kuba Nawieśniak
Kuba Nawieśniak - avatar