placing <script> tags right before </body> doesn't work as expected

placing thesaurus
placing meaning
placing meaning in tamil
placing finance
replacing
how to pronounce placing
placing in marketing
placing reflex

Im watching a tutorial in which the mentor suggests that in order to improve the user experience it is better to add <script> tags right before the </body> , so the script runs after the page content is fully loaded.

I try this block of code and I expect to see the "hello world!" before any script runs but my browser runs the javascript before everything else shows up (displays only a blank page with the alert box)

<html>
 <body>

  <p>hello world!</p>

  <script>
   alert('hey there');
  </script>

 </body>
</html>

I tried this with safari/chrome/opera and got the same result. I know I can use jQuery to solve this but can someone explain this behaviour?


Thanks to @T.J.Crowder , I marked the correct answer, just in case anyone is curious about what is happening under the hood I've found a good explanation here: Script is before /body but it runs before page is loaded

...so the script runs after the page content is fully loaded.

Correct, all the elements above it are parsed and in the DOM — but, the browser may or may not have had a chance to render them yet. To give it a chance to do so, through a very small delay into your code.

    <script>
    setTimeout(function() {
        alert('hey there');
    }, 50);
    </script>
</body>

Live Example:

<p>hello world!</p>
<script>
setTimeout(function() {
    alert('hey there');
}, 50);
</script>

Placing Synonyms, Placing Antonyms, synonyms: find employment for, find a job for, find a home for, accommodate, find accommodation for, allocate, assign, appoint Define placing. placing synonyms, placing pronunciation, placing translation, English dictionary definition of placing. n. 1. a. An area with definite or indefinite

When a script tag is placed at the bottom of the body tag, it simply means that the rest of the DOM is processed before the script runs. It does not guarantee that the page has fully rendered.

You can use the load event to ensure that the page has rendered:

<html>
 <body>

  <p>hello world!</p>

  <script>
   alert('hey there');
   
   window.onload = function() {
      alert("Page has rendered");
   };
  </script>

 </body>
</html>

Placing Synonyms, Placing Antonyms, identify or classify as being of a specified type or as holding a specified position in a sequence or hierarchy. Placing definition: a method of issuing securities to the public using an intermediary , such as a | Meaning, pronunciation, translations and examples

You can use

document.addEventListener("DOMContentLoaded", function() {
  alert('hey there');
});

It is the vanilla js equivalent of jquery document ready

Edit: main difference from window onload in Nisarg Shah's answer that this doesnt wait until the site is fully loaded (external styles, images etc)

placing, synonyms: rank, order, put in order, grade, group, arrange, sort, class, classify, categorize, put, set, assign 74 synonyms of placing from the Merriam-Webster Thesaurus, plus 145 related words, definitions, and antonyms. Find another word for placing. Placing: to arrange something in a certain spot or position.

Placing, verb (used with object), placed, plac·ing. to put in the proper position or order; arrange; dispose: Place the silverware on the table for dinner  From Longman Dictionary of Contemporary English placing plac‧ing / ˈpleɪsɪŋ / noun [ countable ] British English     the position of someone or something in a competition or ordered list   Hancock and Smith took the top two placings.

PLACING, Synonyms for placing at Thesaurus.com with free online thesaurus, antonyms, and definitions. Find descriptive alternatives for placing. 1 mass noun The action of putting something in position or the fact of being positioned. ‘the placing of the lights’ ‘Then there is field placing; you can position a fielder such that you are not expecting them to take a catch or save runs but merely to make the batsman play differently.’

Placing, 74 synonyms of placing from the Merriam-Webster Thesaurus, plus 145 related words, definitions, and antonyms. Find another word for placing. place (someone) To recognize and remember who someone is. They showed me a picture of a suspect and asked if I knew who it was, but I couldn't place her. A: "Do you remember my friend Tom?"

Comments
  • Possible duplicate of Make sure a Javascript script is first to run?
  • I guess "the page content is fully loaded" is an overstatement. It's not fully loaded, it's just interpreted and the elements exists there for you to use but doesn't mean it's rendered.
  • stackoverflow.com/questions/807878/… Check out this link. It answers your question.
  • Most modern browsers wait until the script finishes before rendering the page.
  • @sodali_ayran - That's asking a different question, and while most of the solutions there will work (because they suggest a poor antipattern, using the window load event), it's not a good candidate for closing this question.
  • load happens very late in the page load process, after everything (including all images) have been loaded. It's generally not what you want (though it could be in this specific case).
  • Do note that DOMContentLoaded doesn't wait for dependent resources to load: developer.mozilla.org/en-US/docs/Web/Events/DOMContentLoaded
  • ...and may well fire before rendering occurs, just as it does in the OP's example.