Title: onload event in body element and rendering sequence
Tags: javascript;html;css;dom-events
Question: I'm a new to css, DOM, rendering.
We know that onload can be placed in body tag, make sure we get an element before it exists when using Javascript such as:
document.getElementById(&quot;firstParagraph&quot;)
But what's the sequence of DOM objects when they are created? To put it in other way, is body object created before or after its child elements such as headings, paragraphs?
Some responses said when body tag is created, its child tag hasn't been created, so if I put the onload attribute in body tag to call a function to check the paragraph, it will throw an error since paragraph object hasn't been created? then why we put onload in the body tag?
Comment: @thaveethugce then when body tag is created, its child tag hasn't been created, so if I put the onload attribute in body tag to call a function to check the paragraph, it will throw an error since paragraph object hasn't been created?

Comment: elements are parsed in DOM traversal order. That means tags are rendered the moment parser gets to them starting from the top of the document while respecting parent-child hierarchy. For  parsing a static HTML, there is nothing much counter intuitive about it.  Watch out for async script tags though

Comment: body is the direct child of html, so its created before its child elements, while creating body tag, onload hook willl be called

Comment: read this http://www.breck-mckye.com/blog/2014/04/document-loading-and-DOM-lifecycle-events/ , at the time parsing scripts are identified



{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 0, 'CommentCount': 4, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2018:00:05/05/18', 'FavoriteCount': 1.0, 'PostTypeId': 1, 'Tags': '<javascript><html><css><dom-events>', 'Title': 'onload event in body element and rendering sequence', 'ViewCount': 61, 'id': 50187646, 'user_AboutMe': None, 'user_AccountId': 13332616.0, 'user_DisplayName': 'amjad', 'user_Reputation': 3208.0, 'user_Views': 641.0, 'user_id': 9623401.0}}