Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8085283
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T18:03:54+00:00 2026-06-05T18:03:54+00:00

The order of execution of html/javascript , as far as I know , is

  • 0

The order of execution of html/javascript , as far as I know , is serial (as in all programming language) , meaning the browser reads line by line of html/javascript code and interprets it .

That is why some javascript programmers put the tag at the end of the body – to make sure the entire DOM was loaded and they can now access whatever elements they want .
This is how they do it –

<html>
...
<body>
...
<script> //whatever code you want to put
</script>
</body>

I wanted to test this so I wrote the following code

<html>
<head>
</head>
<body>
<script>
var elm=document.getElementById("myp");
alert(elm.innerHTML);
</script>
<p id="myp"> well this is darned interesting </p>
</body>
</html>

As you can see I’ve put the script before the p element is defined and the script is trying to access it . I thought I would get an error since p isn’t supposed to be defined yet but I got “well this is darned interesting” (the value of p) .
The same thing happens when I put the script in the head element. (testest with chrome and firefox).

Can anyone shed some light into what’s going on ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T18:03:55+00:00Added an answer on June 5, 2026 at 6:03 pm

    Putting aside the specifics of what’s going on with this particular example, I’ll summarize the general principle. My source is Flanagan, JavaScript: The Ultimate Guide, 5/e.

    It’s too simplistic to think of a browser as reading a line of HTML and rendering something in response. Certainly it scans the input sequentially, but the sequence of DOM object construction is not line-for-line with the scanned input. Better to treat it as a black box.

    If you want to run a script that will manipulate DOM elements, the safe and sure way is to run it as a handler for the onload event. Sometimes you can write inline script to do it, but this is a gray area. Flanagan writes:

    Most browsers seem to allow scripts to manipulate any document
    elements that appear before the <script> tag. Some JavaScript
    programmers do this routinely. However, no standard required it to
    work, and there is a persistent, if vague, belief among some
    experienced JavaScript coders that placing document manipulation code
    within <script> tags can cause problems…. (p. 256)

    However, you may need to manipulate the DOM after all the HTML has been parsed but before all images are loaded. If that’s your need, then a tool like jQuery and its ready event is the way to go.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

what is the order of execution in web ? PHP, HTML, Javascript, css and
I followed this post http://neurochannels.blogspot.com/2010/05/how-to-run-r-code-in-matlab.html , to install R(D)Com server in order to call
In regards to the order of execution of statements in SQL, is there any
Is there any way to control updating order during execution of TryUpdateModel ? I
Does AtomicInteger provide any kind of fairness guarantee? like first-come-first-serve execution order of threads
Could anyone explain how Java executes this code? I mean the order of executing
I'm trying to move all of my javascript includes to the bottom of the
I have built a javascript/css dependency manager similar to http://code.google.com/p/jingo/ . It allows me
I'm trying to exactly figure out the execution order of what is going on
HTML Code <div id=foo> <h1>foo</h1> <p>Pellentesque habitant morbi tristique.</p> </div> <div id=bar> <h1>bar</h1> </div>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.