Execute When Finish Load Without jQuery
Execute When Finish Load Without jQuery
Replacing Query with Vanilla JavaScript
// With jQuery
1
2
3
$(document).ready(function() {
/* Do things after DOM has fully loaded */
});
// Without jQuery
1
2
3
4
5
6
7
8
9
10
11
const onReady = (callback) =>{
if (document.readyState!='loading') callback();
else if (document.addEventListener) document.addEventListener('DOMContentLoaded', callback);
else document.attachEvent('onreadystatechange', function() {
if (document.readyState=='complete') callback();
});
};
ready(() => {
/* Do things after DOM has fully loaded */
});
Cheat sheet for moving from jQuery to vanilla JavaScript
// With jQuery
1
2
3
$(document).ready(function() {
/* Do things after DOM has fully loaded */
});
// Without jQuery // Define a convenience method and use it
1
2
3
4
5
6
7
8
var ready = (callback) => {
if (document.readyState != "loading") callback();
else document.addEventListener("DOMContentLoaded", callback);
}
ready(() => {
/* Do things after DOM has fully loaded */
});
This post is licensed under CC BY 4.0 by the author.