简体   繁体   中英

Load the all css & js for the page before downloading the “big list of images”

I have a website that displays a list of around 20 auto-played slideshows(each containing 4-5 images) in a page. Now the problem is that this is taking up a long time to load the website(& ofcourse it would, since so many images) but I am wanting to preferentially load all the neccessary css and js files at first, so that the other components on the page are properly rendered before the images begin to download.

So... Is there a way to set a preferential order of loading of the requested resources ?

Is there a way to set a preferential order of loading of the requested resources

Yes - put them in that order in your HTML file!

I am wanting to preferentially load all the neccessary css and js files at first, so that the other components on the page are properly rendered before the images begin to download.

That's a different problem.

Your page components cannot be properly rendered until the dimensions of most elements are known. So, if you have other images in your page layout, those need to get loaded first, or have their width and height attributes specified in the markup.

Since your slideshow images would be mixed in with the rest of your layout if you used pure HTML to load them it sounds like you have little choice but to instead load all of your slide show images from Javascript, per other comments and answers.

Better way then using the dom to create elements have your divs or even img tags src be blank then load them with the images you want in an onload function using innerHTML:

function images(){
  var place_to_put_image = document.getElementById("image_div");
  place_to_put_image.innerHTML = "<img src=\"your_image_here\"></img>"; 
}

<body onload="images()>
  <div id="image_div></div>
</body>

you could put your images in an array and assign the innerHTML of the div or whatever to the array as well if you want them all in the same place or you could put the onload in div or where ever you want the images to load.

There a few ways you can go about doing it but they all involve not actually putting the images in the HTML - at least not correctly. One way that I like to do it is to add the image src URLs to another property like the alt (not great for accessibility) or rel . Also, give all the images a class that will be used to hook into it with JS.

<img alt="images/foo.jpg" class="loadlater" />

Then in your JS, get all elements with class="loadlater" and set the source to the alt value.

img.src = img.alt;

You didnt mention jQuery. If you were using jQuery, the command to swap the attributes would look like this:

$('.loadlater').each(function() { this.src=this.alt; });

Then, wherever and whenever you call this, the images will be loaded. You will want to call it after your css and js finishes loading - probably the document.load function. Again, if you were using jQuery, it would look like this:

$(function()
{
    $('.loadlater').each(function() { this.src=this.alt; });
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM