We were working on client’s site to optimize its front end performance. Home page had 7 ads placed via AdButler. As a general practice we don’t recommend to have this many ad slots, specially on home page. This is because the 3rd party ads load too many files and add significant delays to page load time for visitors. The revenue of the ads was very important for client so the slots could not be reduced. Not all the slots were in first fold so that gave us an opportunity to optimize performance while still having same number of ad slots. To avoid loading all ads on page load we modified the AdButler code snippet. We made it only loads when slot is in visible area after user scrolls. I am sharing the modified code as it may benefit some sites which have lot of ad slots from AdButler. Here is the original sample slot

The code above initializes the AdButler and renders the ad, the line #7 is responsible for rendering the ad. Now what we had to do was to to execute that line only when users scrolls to specific position. So we removed that line from the code snippet so the updated code looked like

We did same to another slot. With help of jQuery we were able to trigger that line when user scrolled to a specific position. In this case we loaded 1st slot when scroll is greater than 800, and 2nd slot gets loaded after 4300. You may need to adjust as per you design. Here is the code which does the magic.

The variable window.loadSecondAd is used to avoid loading ad more than once. Using code above we were able so save bandwidth consumed by 3 slots on page load so user page load time improved.