28
loading...
This website collects cookies to deliver better user experience
"Lazy loading is a device pattern commonly used in computer programming and mostly in
web design and development to defer initialization of an object until the point at
which it is needed."
lazy_load_css(['style.css', 'styling.css','design.css'])
and when lazy loading only one of them, we will write lazy_load_css("style.css")
, Cool 🆒function lazy_load_css(path){
//The function responsible for lazy loading our css files
let type_of_path = typeof path; //This refers to the type of the path. It can be string or object
switch (type_of_path){
case "object":
var head = document.getElementsByTagName("head")[0]; //Get's the head element
for (let i = 0; i < path.length; i++){
//Loops through all the paths
var link = document.createElement("link"); //Creates a link element
link.href = path[i]; //Assigns a path to the tag
link.rel = "stylesheet"; //Set it's rel to stylesheet
link.type = "text/css"; //Set's it's type to text/css
head.appendChild(link); //Appends it to the head of the document.
}
break;
case "string": //If the path is a single path
var head = document.getElementsByTagName("head")[0]; //Get the head element from the html document
var link = document.createElement("link"); //Create the link element
link.href = path; //Assign the value of path to the link's href
link.rel = "stylesheet"; //Assign 'stylesheet' to the link
link.type = "text/css";
head.appendChild(link); //Appends the link to the head of the document.
break;
}
}
lazy_load_css
which takes a parameter path
.type_of_path
which stores the type of thetype_of_path
variable. Let's look at the first case, case "object". The code below will only be executed if the path is an array or an object. Meaning your array contains many paths. We then define a variable head which is the head element of the html document.<link>
element an assign it to a variable called link<link>
's href.<head>
. <!DOCTYPE html>
<head>
<script type="text/javascript" src="script.js"></script>
</head>
<body>
<h1>Hello World</h1>
<div>
<p>
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</p>
</div>
<script type="text/javascript">
window.onload = function(){
let css_paths = ["design.css","style.css","styling.css"]; //The paths to the css files we want to lazy load.
let css_path = "design.css"; //If we want to lazy load only one stylesheet, this will be the code
lazy_load_css(css_paths); //Initialize the function with the paths.
/*
If we want to lazy load only one style-sheet, this is how we would do
it. lazy_load_css(css_path)
OR
lazy_load_css("styling.css");
*/
}
</script>
</body>