Note that there are some explanatory texts on larger screens.

plurals
  1. POPlace one image from JavascriptObject to multiple places
    primarykey
    data
    text
    <p><strong>Situation</strong>: I have a tiny http server that can only handle 4 connections at any given time. When opening a web page, my web browser sends a get request for every image resource asynchronously (tries to open more than 4 connections at the same time). This causes some bad behaviour.</p> <p><strong>Initial solution</strong>: I wrote a JS function that loads the images sequentially and stores them in a dictionary </p> <blockquote> <p>var loadedImages = {};</p> </blockquote> <p>like so:</p> <pre><code> var img = new Image(); img.src = &lt;img_location&gt;; loadedImages[&lt;img_name&gt;] = img; </code></pre> <p>After all the images are loaded i try to place them in various places in the DOM. The important part is that i need to place the same picture in multiple places. I do it like this:</p> <pre><code>//For all the elements that need to have the same image DO: var img = loadedImages["&lt;img_name&gt;"]; $(this).html(img); </code></pre> <p><strong>Problem</strong>: What happens is that as soon as the code puts the image in the SECOND element, the image gets removed from the FIRST element. When the image gets put in the THIRD element, it gets removed from the SECOND element. So what happens is that basically only the last element contains the image, while all the others are empty.</p> <p><strong>Question</strong>: How can I place the same image from my javascript dictionary (or any other javascript object) on multiple DOM elements?</p> <p><strong>Edit</strong>:When using something like</p> <pre><code>//For all the elements that need to have the same image DO: var img = loadedImages["&lt;img_name&gt;"]; $(this).html($(img).clone()); </code></pre> <p>as proposed by <em>Tamil Vendhan</em> and <em>Atif Mohammed Ameenuddin</em>, the image gets placed on all the elements and that is ok, but the browser requests the image from the server every time it comes to that line of code. So it is not really a good solution. Same goes when i use "cloneNode()"</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload