How to tell if this script works?

Hi all. I am attempting to defer image loading via this method to my WordPress site.

This method says to add the following to my images:

<img src="data:image/png;base64,R0lGODlhAQABAAD/ACwAAAAAAQABAAACADs=" data-src="your-image-here">

Which I did:

[caption id="attachment_4768" align="alignnone" width="110"]<img class="processimages wp-image-4768" src="data:image/png;base64,R0lGODlhAQABAAD/ACwAAAAAAQABAAACADs=" alt="Discovery" width="110" height="110" data-src="http://www.modovis.com/wp-content/uploads/2015/12/Process-DYIcon600-1.png" /> Discovery[/caption]

Then I added the required script:

<script>
function init() {
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'));
} } }
window.onload = init;
</script>

I added it into my existing script sequence (last one):

<script type="text/javascript" language="javascript">
   
jQuery(document).ready(function($) {

  $(".toggle").click(function() {
           var $next=$(this).next().toggle(400);
          $('.answer').not($next).hide();
    });
  $('ul.tabs li').click(function(){
		var tab_id = $(this).attr('data-tab');

		$('ul.tabs li').removeClass('current');
		$('.tab-content').removeClass('current');

		$(this).addClass('current');
		$("#"+tab_id).addClass('current');
	})
$(".desc").hide();
    $(".desc:first").show(); // THIS LINE IS ADDED!!!
$("h3.open-close").click(function(){
if ($(this).is(".current"))
{
$(this).removeClass("current");
$(this).next(".desc").slideUp(400);
}
else
{
$(".desc").slideUp(400);
$("h3.open-close").removeClass("current");
$(this).addClass("current");
$(this).next(".desc").slideDown(400);
}
});
$(window).scroll(function() {

    if ($(this).scrollTop()>0)
     {
        $('.nextscreen').fadeOut();
        $('.arrowsubmenu').fadeOut();
     }
    else
     {
      $('.nextscreen').fadeIn();
      $('.arrowsubmenu').fadeIn();
     }
 });
$('#quantity,#apply_time').keyup(function() {
   var units = parseInt($("#quantity").val().replace(',', ''));
   var seconds = $("#apply_time").val();
   var convert = 60;

   var total = Math.round (units * seconds / convert / convert);

   $("#hours").val(total); // sets the total hours input to the quantity * seconds
});
function init() {
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'));
} } }
window.onload = init;
});

</script>

If you are still with me, here are my questions:

  1. Does my code, both HTML and JS look correct? Do I need another closing tag after the last script?

  2. How do I know this actually defers the loading? I know it sounds like a stupid question, but Im not sure how to tell if the images are loading after the page or during as before.

Here is the test page: Defer

Yes. No.

[quote=“dave8794, post:1, topic:228269, full:true”]
2. How do I know this actually defers the loading?[/quote]

Because the window.onload event only occurs after the rest of the page has loaded.

[quote=“dave8794, post:1, topic:228269, full:true”]
I know it sounds like a stupid question, but Im not sure how to tell if the images are loading after the page or during as before.[/quote]

You can investigate the network section of your developer tools. For example, here you can see that your Process-DY icon hasn’t loaded until after the red line, which is when the onload event triggers.

1 Like

Paul, thank you so much for taking the time to look over my code and explain this! I have never used the Network section, that provides some great insight. Everything you said makes sense and I’m thrilled it works!

I do have one additional issue. Everything works fine on my test page. All 5 images defer:

Here is the test page: Defer

I took the exact same code and applied it to my main page. On the main page, only the first image defers:

The other 4 don’t

https://snag.gy/GuCqwj.jpg

I’m not sure why this is happening.

Main Page

You have forgotten to update the src attribute to the data:image/png string.

Hi Paul, sorry for the late response, i didn’t get your notification. How do I have to update src attribute? I don’t understand. Also, I did get everything working before reading your response. All images deferred fine. Problem is it actually slowed down my page load. Perhaps because I didn’t update the src?

That’s a definite. See your post #1 for what to use for the src attribute.

I did use that src attribute in post 1 on all the images. I think my problem was I have those same 5 images used in divs that are only shown on mobile. The duplication of files may have been the problem because i got it all to work once I deferred those mobile divs too. If you recently looked at my source code, the updated src from post 1 is not there, I changed it back because deferring images load was slowing down my page loading which is defeating the purpose. Im sure I was still doing something wrong, I don’t know why it slowed my loading.

Please provide a link that demonstrates the problem.

Here is the page with the deferred images.

Thanks. I’m seeing from my desktop machine that the process images are being correctly deferred.

Can you please expand on the nature of the problem?

Sure, now that the images are deferred, my pageload time has seems to have increased, not sure why. I have tested on Pingdom, GTmetrix, and in Developer Tools > Network.

I suggest that you create several versions of the page and then test them against each other. That way you will have actual metrics that can be used to judge between them.

Actually I did, and the deferred page has a longer load time. I’ll set it up again see what happens. Thanks!

If the deferring is being done in the same page as displaying them then the script to defer them will add to the total load time.

We need to find out if it’s the onload time or the total load time that is of interest here. If the latter, then the definition of the word “defer” needs to be appreciated…

1 Like

I thought about that. The deferring is in fact being done on the same page displaying the images. If its the case the script is adding time, its making the purpose of deferring moot.

Its the page load time. The red line in the Network link of Developer Tools. Not the DOMContentLoaded or the Finish time.

Let’s get some test pages up. All else is moot until actual numbers are acquired.

Will do!