Languages
[Edit]
EN

JavaScript - on style load event

0 points
Created by:
Dragontry
731

In this article, we would like to show you how to handle style load event using JavaScript.

Quick solution:

// ONLINE-RUNNER:browser;

<style>
    /* web page styles here */
</style>
<script>
    var style = document.querySelector('style');
    style.onload = function load() {
        console.log('The style has been loaded successfully.');
    }
</script>

or:

// ONLINE-RUNNER:browser;

<style>
    /* web page styles here */
</style>
<script>
    var style = document.querySelector('style');
    style.addEventListener('load', (event) => {
        console.log('The style has been loaded successfully.');
    });
</script>

 

Practical examples

Example 1

In this example, we add a function to the window.onload property, which will be executed on successful script loading.

// ONLINE-RUNNER:browser;

<!doctype html>
<html>
<head>
    <style>
        /* web page styles here */
    </style>
    <script>
        var style = document.querySelector('style');
        style.onload = function load() {
            console.log('The style has been loaded successfully.');
        }
    </script>
</head>
<body> Web page body... </body>
</html>

Example 2

In this example, we add an event listener to the window object, which executes a function on a script load event.

// ONLINE-RUNNER:browser;

<!doctype html>
<html>
<head>
    <style>
        /* web page styles here */
    </style>
    <script>
        var style = document.querySelector('style');
        style.addEventListener('load', (event) => {
            console.log('The style has been loaded successfully.');
        });
    </script>
</head>
<body> Web page body... </body>
</html>

References

  1. GlobalEventHandlers.onload - Web APIs | MDN

Alternative titles

  1. JavaScript - on style load success event
  2. JavaScript - style onload event
  3. JavaScript - style loaded event
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join