Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

225
Views
Javascript - Lazy Loading for iFrame (iFrame re-uploads every time I scroll up or down)

Having issues with my Lazy Load code:

HTML

<div class="mapFrame mobileHide" >
     <iframe class="lazy" data-src="{eval var=$config.gisMapUrl}" id="mapIFrame" ></iframe>
</div>
...
<div class="mapFrame mobileHide" >
    <iframe class="lazy"  data-src=="{eval var=$config.polUrl}" ></iframe>
</div>

Javascript

document.addEventListener("DOMContentLoaded", function() {
  var lazyloadiFrame = document.querySelectorAll("iframe.lazy");    
  var lazyloadThrottleTimeout;
  
  function lazyload () {
    if(lazyloadThrottleTimeout) {
      clearTimeout(lazyloadThrottleTimeout);
    }    
    
    lazyloadThrottleTimeout = setTimeout(function() {
        var scrollTop = window.pageYOffset;
        lazyloadiFrame.forEach(function(iframe) {           
            if(iframe.offsetTop < (window.innerHeight + scrollTop)) {
              iframe.src = iframe.dataset.src;            
              iframe.classList.remove('lazy');  
             
            }
        }  );
        
    }, 20);
  }
 
  document.addEventListener("scroll", lazyload);
  
});

It works like a charm when the page loads; and the iframes do not load at all until I scroll to them. The issue is... Once I scroll to the iFrames, and display the iFrames. I use the scroll bar to move up or down, the iFrames' content keeps reloading itself again and again... Each time I move the scroll bar, it reloads. I need to stop once loaded and just display. Basically, once the scroll bar hits an iframe, iframe loads the content, displays the content and I can continue scrolling without it reloading the content.

Reloading takes too much time, and there is also a focus within the iframes content, which causes it to go to that particular iframe whenever you hit the scroll bar, and reloads it again. I need to have just have the iframe to display the content once it is loaded, and I can continue scrolling up or down without reloading it.

I tried removing the class="lazy", and it does remove the class but the content still reloads every time I hit the scrollbar. I tried removing scrollTop, but that just seems to remove the content within an iframe.

I am a bit rusty with javascript coding and anyone's help is appreciated especially in coding what to do next. Please help!

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!