Skip to content

Instantly share code, notes, and snippets.

@gr1zix
Created September 12, 2021 21:08
Show Gist options
  • Save gr1zix/24e06e9d57c76a4bee6e2b70c5ebb21b to your computer and use it in GitHub Desktop.
Save gr1zix/24e06e9d57c76a4bee6e2b70c5ebb21b to your computer and use it in GitHub Desktop.
Async custom or external scripts & styles loading which need to be used after website interact (Best way to load big files like lottie scenes on Base64 stored in json which have size ~10M)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script type="text/javascript">
// Async custom or external scrips & styles loading
(function() {
var lazyLoaded = false;
window.addEventListener("scroll", function () {
((document.documentElement.scrollTop || document.body.scrollTop) > 0 && lazyLoaded !== true) &&
(!(function () {
var genericIcons = document.createElement('link');
genericIcons.href = 'https://myapp.com/content/icons-pack-from-footer.css';
genericIcons.rel = 'stylesheet';
genericIcons.type = 'text/css';
genericIcons.media = 'all';
var linkItem = document.getElementsByTagName('link')[0];
linkItem.parentNode.insertBefore(genericIcons, linkItem);
})(), (lazyLoaded = true));
}, true);
})();
</script>
</head>
<body>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment