I didn't have much use for loading ads after my content, since on most of my sites and those I worked on, they were near the end of the code anyway, but while working on a site today I ran into a problem. There is an ad at the top of the design which tends to load slowly.
I have specific requirements that the site should be as fast as possible, so I had to devise a way of loading the ad last. I went through some methods I found online after things off the top of my head didn't work, but none helped, so I came up with my own code for it.
I am not taking credit for this, so if you did this before me feel free to let us know, but I did arrive at it on my own. The methods you could try is the "defer" attribute, which defers loading to the end of the page load, however, this had no effect at all. You can also try a "window.onload", but this didn't help either. With any other function combinations the ad either loaded as usual or did some weird stuff.
In the end, I decided to load the ad at the bottom of the page, right before the "" tag, and then use JavaScript to "transfer" the contents to where I need it do be, while keeping the source hidden. This is achieved using the "document.getElementById" method, read on to take a look at a specific example.
So what we need to do is create a div at the bottom of our page, load the advertisement there, and transfer it to the place we want it to be. I will be using inline CSS and inline javascript, which should not be done in real "life", the best practice is to have all your CSS in external stylsheets and you javascript called from files in the header (where possible). The bottom of our page would look like this:
<div id="top_ad_loader" style="display:none;">
Ad code in here
</div>
<script type="text/javascript">
document.getElementById("top_ad").innerHTML = document.getElementById("top_ad_loader").innerHTML
</script>
So what is going on here? We have two divs, the first is "top_ad_loader", which you can see right here. This div is hidden, but contains the ad code. When the code is being read, the JavaScript might take a while to load, but we're right at the bottom, so all our content is displayed already. There is also another div, "top_ad", which can not be seen here, it is somewhere way above this part of our code, somewhere near the "<body>" tag.
There is JavaScript code right beneath the ad loader, the purpose of which is to transfer the loaded contents of the ad loader to te place where we want it to be. We "grab" the contents inside the ad loader using "document.getElementById("top_ad_loader").innerHTML", and we want the contents of the actual ad block to equal this.
Once the page load gets to the ad it will slowly load it, when finished, parsing will continue, and our JavaScript will transfer the contents to the top.
If you have a page that loads a bit slowly, perhaps this method would be worth a try? Contents usually load faster than JavaScript, so if you place the JavaScript load last your content will load in 1-2 seconds (maybe much less), making the JavaScript load 1-2 seconds later. However, if a JavaScript at the beginning loads in 5 seconds, you need to wait that out just to start loading the content.
Ghacks needs you. You can find out how to support us here or support the site directly by becoming a Patreon. Thank you for being a Ghacks reader. The post Load your advertisements after your content appeared first on gHacks Technology News.