How to make an java scritpt AD to load only after page is fully loaded?

MartonB

Regular Member
Joined
Aug 20, 2019
Messages
285
Reaction score
70
Hello

I'm trying to help my friend to improve his page load,

He has an external JS that takes some time to fully load.

we d'ilke to make that JavaScript load after the page is fully loaded

Please any idea how to do that? thanks
 
Oh shuuuuuuuuuuuuuuuuuuuuuuiiit, no one?
 
Something you can also try is adding async to your script tag, eg:

Code:
script src="myscript.js" async
 
try this

JavaScript:
$(document).ready(function() {
    $.getScript("http://jact.atdmt.com/jaction/JavaScriptTest");
});

source: https://stackoverflow.com/questions/19737031/loading-scripts-after-page-load
jQuery in 2020
:confused:
 
Here's a native version (which you can embed into a window.onload or DOMContentLoaded listener:

Code:
(function(document) {
    var newScriptTag = document.createElement('script'); // creates the script tag
    var firstScriptTag = document.getElementsByTagName('script')[0]; // find the first script tag in the document
    newScriptTag.src = 'myscript.js'; // set the source of the script to your script
    firstScriptTag.parentNode.insertBefore(newScriptTag, firstScriptTag); // appends the new script to the DOM
}(document));
 
Good point. defer keeps the order of execution the same - so if there are other external scripts dependent on this script then defer is important. Also defer waits until the DOM is ready, which may improve the page load speed further than just using async.
 
I once heard Doug Cannington mention some services that can do this. I didn’t grasp the name or the concept. He has many live sessions weekly; you can try to catch up with him and ask. Meanwhile, you may also look into manual customization of your HMTL codes of your CMS to delay loading.
 
Back
Top