0

I have a javascript and I don't know where excatly I should insert window.onload should I place it in the middle or before can any one guide me to the right placement ??

     Javascript:

     window.onload = function() {

    const printSentence = (id, sentence, speed = 50) => {
   let index = 0;
   let element = document.getElementById(id);

  let timer = setInterval(function() {
  const char = sentence[index];
 
  if (char === '<') {
  index = sentence.indexOf('>', index);  // skip to greater-than
}

element.innerHTML = sentence.slice(0, index);

if (++index === sentence.length) {
  clearInterval(timer);
}
 }, speed);
} // printSentence

  printSentence(
  'one',
  '<p>hello</p>',

  50
 );
 }
Mar
  • 53
  • 7
  • 1
    You can put it wherever you want as long as nothing else assigns to `window.onload`. Or you could avoid it entirely by giving the script a `src` and `defer` attribute, or by putting it at the bottom of the `` – CertainPerformance Oct 31 '20 at 01:18

1 Answers1

0

Keep this script tag at the end (at the bottom of the body) to make sure everything in the body has loaded.

Arpan Kc
  • 928
  • 1
  • 6
  • 19