Questions? Call (860)407-2687
find us on:

Load Web Page Images 🖼 on Page Load using Javascript

short url:
  • Load Web Page Images 🖼 on Page Load using Javascript
  • Javascript Code to Lazy Load Images 🖼 After Page Loads
  • How to Display Loading Image 🖼 or Icon While Page is Loading
  • Free Tool to Create Image 🖼 Code for Delayed ⏳ Loading of Web Page Images
  • Additional Tips to Increase the Speed of Your Web Pages

Load Web Page Images 🖼 on Page Load using Javascript

One of the biggest sized resources of website (next to video) that contribute to a slow website or blog are images.

There are two main ways to prevent images from slowing down the load and display of your web pages:

  • Compressing your websites images as much as possible to reduce their file size.
  • Delaying the loading of the images so images only load after the rest of the page has finished loading. This technique is often referred to as html image lazy loading, although try html image lazy loading shows images on the page as the user scrolls down the page which actually requires quite a bit more code than necessary and remember, the more code, of any kind, the users web browser has to process and interpret, the slower your web pages are going to be. Lazy loading plugins for the purpose of loading images after page load in Wordpress usually use Jquery to do this and this is a whole lot of code the visitors web browser has to process unnecessarily.

Both of these are important for web page speed but delaying the loading of your web pages images is what this web page is about. This can easily be accomplished via Javascript with a fairly short piece of simple Javascript code.

Javascript Code to Lazy Load Images 🖼 After Page Loads

The delay of loading images does not have to be a long delay at all to make a significant impact on our Google Page Speed score.

A mere couple of seconds delaying in the loading of images is all that is required. Our goal here is not only to increase our Google Page Speed score but to make it so the website visitor can immediately begin scrolling your web page and interacting with it.

There are two steps to delay the loading of your web pages images:

  1. Remove the src attribute of your images and change it to data-src. The src attribute of an image is the url or web address of where the image resides on the web server. By changing the src to data-src, as the page is loading, the image code does not attempt to download the image from the web server, thereby removing that web page load speed bottleneck.
  2. After the web page loads, a Javascript timer, which is set to fire a couple of seconds after the rest of the web page loads, changes the data-src attribute to the src. When that occurs, the images then load.
Change Image src to data-src
Image of HTML code where src of image is changed to data-src.
Change Image src to data-src

Here is the Javascript code for this. We don't want to use Jquery to do this because this means we are violating a page load speed rule of eliminating the web pages external dependencies. Jquery requires a call to an external Javascript file, unless you download Jquery, but click this link to the Jquery code and see how much code is there compared to the little bit of code below.

Jquery Just for Purpose of Lazy Image Load is too Much Code
Image of massive amount of code contained within the Jquery library.
Jquery Just for Purpose of Lazy Image Load is too Much Code

This Javascript code below can be placed in a Javascript file on YOUR web server of placed directly within the web page.

The advantage of putting it in a Javascript file on your web server is if you want to make changes to it, you don't have to change code on all the pages on your website, just change the code in the file.

You can view a demo page which shows you this delayed image loading.

Each line of code is explained so you understand exactly how the Javascript source code works to lazy load or delay loading of your web pages images.

sel
javascript
 
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
setTimeout(function(){deferImagesLoad();},1700);
 
function deferImagesLoad(){
   var imgDefer = document.getElementsByTagName("img");
 
   for (var i = 0; i < imgDefer.length; i++){
          if(imgDefer[i].getAttribute("data-src")){
              imgDefer[i].setAttribute("src", imgDefer[i].getAttribute("data-src"));
          }
     }
}
 

This code should be placed at the very bottom of the web page, right before the closing </body> tag. The reason we want this code to be at the bottom of the web page is so this code does not run until all the other web page code is processed, meaning, the page has finished loading.

How to Display Loading Image 🖼 or Icon While Page is Loading

The delay of the loading of images is so short that showing some sort of image loading message is probably not needed, but top show loading image while page loads can easily done by simply specifying the src attribute of the image to be a placeholder image which would be a transparent image with the text image loading ... on it. An image like this would be extremely small file-size-wise, so it would not delay the loading of the web page.

You can view a demo page which shows you this delayed image loading with IMAGE LOADING placeholder image.

The Javascript code above would replace the src of the image from the image loading ... placeholder image to the real image src. You can download a image loading ... placeholder image to use if you wish.

Left click the link to the image below if you wish to see what the image looks like before you download it or right click the link below to save the image. The image file size is a mere 5kb. The images background is transparent so your web pages default background color can show through.

Free Tool to Create Image 🖼 Code for Delayed ⏳ Loading of Web Page Images

You can use this free tool to create the image HTML code for the delayed loading of the images on your web page. Make sure to fill out all the text boxes below as they are important for search engine optimization on your website.

Don't forget to include the above Javascript code for the delayed loading of your web pages images after the web page loads. The Javascript code should be placed at the bottom of the web page right before the closing </body> tag. This ensures the code runs only after the rest of the web page code has been loaded and processed. No matter which selections you make below, the Javascript code above does not need to be altered for it to work.

Enter URL/web address (on your web server) of image loading ... image IF you're going to use this.
This is the link to download the image loading ... image if you wish. Right click and save as to download: image loading ... image
Enter the URL or web address of the image, that you wish to delay or lazy load.
Enter Image ALT text (short sentence description what the image is).
Enter Image width and height.
 
Enter number milliseconds delay before image loads. For 1 second enter 1000. For 1.5 seconds enter 1500, etc. DO NOT insert commas , in the number.
Image HTML code will appear here. This is the image code to use IF you are NOT going to use an image loading ...image
Image HTML code will appear here. This is the image code to use IF you ARE going to use an image loading ...image
Javascript code to facilitate delay load of image will appear here

CONTENT RELATED TO LOAD WEB PAGE IMAGES 🖼 ON PAGE LOAD USING JAVASCRIPT



Best Ping Services List For Blogs Like Wordpr

... e sends out a ping a maximum of X number of times per hour. In reality, you only should ping when your first publish a post and if you make a major update or change to the post. By default, the Wordpr ...

How To Dramatically Increase The Speed Of You

... web page: The ROOT URL is the web address of the site, not including the page, meaning everything up to and including the .com, .net, etc. For example, the url to THIS page is https://www.scrapersnbo ...

Special Characters And Symbols For Copy Paste

... representation for different kinds of pointers and arrows. Click any of them to get the character code/number to insert in your web page. See ALL of the ARROWS Special Ascii Character Codes for Arrow ...

COMMENTS ON LOAD WEB PAGE IMAGES 🖼 ON PAGE LOAD USING JAVASCRIPT

Feel free to submit your comment below. Anything that does not contribute and is just spam will automatically be deleted. Questions marked by * are required. Comments are checked by a human to make sure they are not spam/automated and are on topic and related to Load Web Page Images 🖼 on Page Load using Javascript.

NAME:
EMAIL:
COMMENT:
HTML OK. Allowable HTML tags include: <p></p>, <i></i>, <b></b>, <em></em>, <strong></strong>, <ul><li></li></ul>, <ol><li></li></ol>
Question ❔
Back to Top