async-script-loader icon indicating copy to clipboard operation
async-script-loader copied to clipboard

Defer loading after the page has been completely rendered

Open raduab opened this issue 4 years ago • 2 comments
trafficstars

In the README it says:

You pass a list of urls to the loader, along with a method for checking that your page is ready, and a callback to call when it is.

The method for checking if the page is ready seems to be actually the method that is checking if the library has loaded, for not loading it twice (the test method).

Is there a way for deferring the loading of the script only after the page has been completely loaded? This is useful for page load speed.

Very nice library, simple and useful - Thanks!

raduab avatar Jun 26 '21 18:06 raduab

I would think you could do

loader(
	[{ type: 'script', url: '/surveillance.js' }],
	() => 'complete' === document?.readyState,
	() => { console.log('surveilled'); }
);

colinhowells avatar Jan 24 '23 22:01 colinhowells

though thinking about it what you might want instead is

document.onreadystatechange = () => {
	if ('complete' === document.readyState) {
		loader(
			[{ type: 'script', url: '/surveillance.js' }],
			() => !!window.surveilled,
			() => { console.log('surveilled'); }
		);
	}
};

colinhowells avatar Jan 25 '23 01:01 colinhowells