load javascript code example

Example 1: javascript document load

window.addEventListener("load", function(event) {
    console.log("Tutte le risorse hanno terminato il caricamento!");
});

Example 2: load js

// Log a message when the page is fully loaded:

window.addEventListener('load', (event) => {
  console.log('page is fully loaded');
});

Example 3: jQuery - AJAX load() Method

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
  $("button").click(function(){
    $("#div1").load("demo_test.txt");
  });
});
</script>
</head>
<body>

<div id="div1"><h2>Let jQuery AJAX Change This Text</h2></div>

<button>Get External Content</button>

</body>
</html>

Example 4: load js

// The same, but using the onload event handler property:

window.onload = (event) => {
  console.log('page is fully loaded');
};

Example 5: load js

window.addEventListener('load', (event) => {
  console.log('page is fully loaded');
});

Example 6: load js

const log = document.querySelector('.event-log-contents');
const reload = document.querySelector('#reload');

reload.addEventListener('click', () => {
  log.textContent ='';
  window.setTimeout(() => {
      window.location.reload(true);
  }, 200);
});

window.addEventListener('load', (event) => {
    log.textContent = log.textContent + 'load\n';
});

document.addEventListener('readystatechange', (event) => {
    log.textContent = log.textContent + `readystate: ${document.readyState}\n`;
});

document.addEventListener('DOMContentLoaded', (event) => {
    log.textContent = log.textContent + `DOMContentLoaded\n`;
});

Tags:

Html Example