Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Defer loading after the page has been completely rendered #9

Open
raduab opened this issue Jun 26, 2021 · 2 comments
Open

Defer loading after the page has been completely rendered #9

raduab opened this issue Jun 26, 2021 · 2 comments

Comments

@raduab
Copy link

raduab commented Jun 26, 2021

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!

@colinhowells
Copy link

I would think you could do

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

@colinhowells
Copy link

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'); }
		);
	}
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants