What is the order in which an HTML page loads?

Can you explain the sequence in which an HTML page loads? Below is a basic HTML outline:

<html>
<head>
    <link rel="stylesheet" href="css/sheet1.css">
    <link rel="stylesheet" href="css/sheet2.css"
    <script src="scripts/take1.js"></script>
    <script src="scripts/take2.js"></script>
<head>
<body>
<button>button1</button>
<img src = "HQ1.jpg" />
<img src = "HQ2.jpg" />
<button>button2</button>
</body>
</html>

What I understand - (please correct me if I am wrong)

The head section loads first, followed by the body section.

My questions -
1. Does the body section start loading only after the head section has loaded completely?
2. Do CSS sheet1 and sheet2 load sequentially, or do they load simultaneously with JS files?
3. Do CSS and JS files load in parallel, or do they load together sequentially?
4. Will button2 display on the page only after HQ1 and HQ2 images are fully loaded due to their larger file sizes?
5. Are HQ1 and HQ2 downloaded concurrently, or is it a synchronous download where HQ1 must finish before HQ2 starts downloading?

Answer №1

When an HTML page is loaded, it is parsed sequentially from the beginning to the end. As resources like stylesheets, images, or scripts are encountered, the browser initiates parallel requests to fetch those resources.

Images and stylesheets do not block the parsing of the page, allowing the rest of the content to be processed while these resources are being fetched.

Script tags that do not have the defer or async attribute specified are considered blocking resources. The browser must load and execute these scripts before continuing with the parsing of the remaining page content.

Does loading of the body section begin only after the head section has been fully loaded?

Yes, but the browser may start parsing the <body> section before all resources in the <head> tag have been fetched. However, all elements in the <head> section are parsed before moving on to the <body> section.

Is CSS file sheet1 loaded completely before sheet2 and JS file start loading?

No, stylesheets are loaded in parallel, and the page does not wait for one stylesheet to finish loading before starting to parse another one.

Do CSS files and JS files load in parallel separately, or do they load together in parallel?

CSS files are loaded in parallel, and multiple script files can also be fetched in parallel. However, the execution of scripts will happen serially unless they have the async or defer attribute. Browsers may optimize performance by fetching resources in parallel based on their look-ahead mechanism.

Since HQ* images are large files, does button2 appear on the page only after HQ1 and HQ2 have finished loading?

No, images are loaded asynchronously and do not block other page content from rendering while they are being fetched.

Are HQ1 and HQ2 downloaded in parallel, or is it a sequential process where HQ1 loads first followed by HQ2?

Images are typically loaded in parallel up to a certain limit determined by the browser's connection constraints. In your scenario, HQ1 and HQ2 would likely be fetched in parallel, but this behavior can vary based on browser implementations.

What is the sequence in which an HTML page loads?

In the sample HTML page provided:

<html>
<head>

    <link rel="stylesheet" href="css/sheet1.css">
    <link rel="stylesheet" href="css/sheet2.css">

    <script src="scripts/take1.js"></script>
    <script src="scripts/take2.js"></script>

</head>
<body>

    <button>button1</button>
    <img src = "HQ1.jpg" />
    <img src = "HQ2.jpg" />
    <button>button2</button>

</body>
</html>

The loading sequence can be summarized as follows:

  1. The browser parses the <html> and <head> tags initially.
  2. External stylesheet references in the <head> trigger server requests, which are initiated concurrently by the browser.
  3. Similarly, external script files referenced in the <head> result in individual server requests launched in parallel.
  4. Script files serve as blocking resources, requiring their download and execution before further processing of the page can proceed.
  5. After the initial script finishes downloading and executing, subsequent actions such as processing additional scripts or handling stylesheets take place.
  6. Parsing continues with the <body> section once the critical rendering path is unblocked by completed resource downloads.
  7. Individual elements within the <body> are processed sequentially, potentially leading to partial rendering during the loading process.
  8. Images like HQ1 and HQ2, though large, load asynchronously and do not hinder the ongoing rendering of the page content.

For further insight into resource loading patterns and timing, utilizing tools like Chrome's Network tab can provide valuable insights through visual representations of resource timelines.

To delve deeper into script loading behaviors, refer to this detailed explanation on the order of script loading, including discussions on the impact of attributes like async and defer.

Answer №2

The structure of HTML documents is intentionally organized in a tree hierarchy. The browser's rendering engine follows this hierarchy by traversing through the tree, parsing elements and adding them to the document before moving on to the next level.

As a result, an HTML page is typically rendered from top to bottom:

Rendering Order:
================

-- <html>
|      <head>
|          ...
|      </head>
|     <body>
|         <div>...</div>
|     </body>
-- </html>

Answer №3

Let's make sure we're on the same page here, we need to distinguish between the HTML page's header/body and the HTTP request's header/body.

Although the HTTP request's header is processed first followed by the body, both are received together in a single HTTP response.

In the same way, the complete HTML page (including header/body tags) is contained within the HTTP response.

Now, onto your query:

The browser parses the HTML page sequentially, from top to bottom, line by line (starting with the first and ending with the last). As it parses the page, the browser identifies the need for additional resources such as CSS, JS, JPG, GIF files, etc., and sends parallel requests to fetch them unless specified otherwise.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the best way to convert an HTML table into an array of objects?

In my Angular Protractor end-to-end (e2e) tests, I need to perform assertions on an HTML fragment similar to the following: <table> <thead> <tr> <th>Name</th> <th>Age</th> ...

Dynamic form population with dropdown selection using Ajax - Coldfusion continuation

Following some valuable feedback from my previous inquiry, I have made progress: Refer to the original post - Coldfusion Populate form with dropdown selection using ajax Currently, I have successfully sent a request to my CFC with a remote function, but I ...

The beforeCreate function is failing to execute when a new user is being created

I'm currently working with sailsjs version 0.11.0. My goal is to ensure that when a new user is created, their password is encrypted before being stored in the database. To achieve this, I have implemented the use of the bcrypt library. In my User.js ...

Creating nested namespaces with interfaces in Typescript type definitions

In my Javascript project, I am trying to define typing for a specific structure. Consider the following simplified example: a | + A.js + b | + B.js Here we have a folder 'a', and inside it there is another folder 'b'. My goal is t ...

Does React trigger a re-render when setState is called even if the state value remains unchanged?

Imagine I create a React component with an initial state of {random: 1}. Now, if I were to execute the following code: this.setState({random: 1}) Would this action result in triggering a re-render of the component? Furthermore, is there a method to avoid ...

How can I use jQuery to pinpoint where my focus currently lies and debug any issues?

Triggering focus() on an element is a simple task, but after encountering debugging problems, I've come to realize that finding out where my focus has shifted can be quite challenging. The issue arises when I'm creating a modal window using jq.UI ...

What is the best way to access a reference to the xgrid component in @material-ui?

Is there a way to obtain a global reference to the xgrid component in order to interact with it from other parts of the page? The current code snippet only returns a reference tied to the html div tag it is used in, and does not allow access to the compo ...

I aim to have just the footer section perfectly aligned within the screen, without the entire body extending beyond

I am encountering an issue with the footer tag once again. I need this section to occupy the entire screen, without affecting the rest of the body content. That is why I omitted setting margin:0; for body elements. I attempted to apply margin:0 to the foo ...

I am unsure of the timestamp format for the value 1522899000000. Can it be used in both Ionic and AngularJS? And how would one go

While parsing a JSON from an external URL, I came across a timestamp with the value starttime: 1522899000000 (on 4/5/2018 -> 5th April 2018). The value seemed like a Unix timestamp, but when I tried to convert it, it displayed the year 1912. What kind o ...

Issue encountered while subscribing to SalesForce topic using Node.js

Attempting to subscribe to a SalesForce topic through a Node.js server using the code provided in the JSForce documentation: conn.streaming.topic("InvoiceStatementUpdates").subscribe(function(message) { console.log('Event Type : ' + message.ev ...

Utilize Javascript to load content dynamically while ensuring each page has a distinct link to individual content pages

As a newcomer to web development, I wanted to share my issue in hopes of finding a more efficient solution than what I've been attempting. Recently, I made changes to my website so that content is loaded dynamically using the jQuery load() function. T ...

The Challenge of Logging in with the Loopback Angular SDK

I have set up a "user" named model with the base class of "User". I'm attempting to log in a user on my Angular App using the service generated by lb-ng, but it doesn't seem to be working. When I call User.login() in my Login controller, providin ...

Create a heading for the selection menu

I need to customize a dropdown list on my page so that the text "Select Language" is always displayed to the user, regardless of whether they make a selection from the dropdown. Even if the user chooses "Option 1" or "Option 2," the default text should sti ...

What are the steps to generate interactive hotspots in a 3D setting with threejs?

I'm new to working with three.js and I'm attempting to create a project similar to the example found at . I have everything set up using three.js, but I'm struggling to figure out how to implement a hotspot (like the red dot in the provided ...

Leveraging TypeScript modules without the need for require()

I created a TypeScript 2.3 app called app.ts that requires the Vue.js library version 2. I manually added the Vue.js script src in the HTML file. All I wanted was to use Vue.js with type checking. I thought I could simply reference it like this: /// & ...

Vue component doesn't update reactively until the template value changes

I have a child component in my Vue application that utilizes Vuex for managing certain values. Currently, when I trigger an action in the parent component, it should also trigger the child component. This is achieved by passing an active boolean prop to t ...

Delete elements with identical values from array "a" and then delete the element at the same index in array "b" as the one removed from array "a"

Currently, I am facing an issue while plotting a temperature chart as I have two arrays: a, which consists of registered temperature values throughout the day. For example: a=[22.1, 23.4, 21.7,...]; and b, containing the corresponding timestamps for eac ...

What is the most efficient way to iterate through array elements within a div element sequentially using React?

Currently, I am working with a large array of 2D arrays that represent the steps of my backtracking algorithm for solving Sudoku. My goal is to display each step in a table format, similar to animations that illustrate how backtracking works. Although I ha ...

Establishing shared variables in node.js

I have a variable defined in my index.js that I need to access from static.js using require() Index.js function API() { var self = this self.init = function(apikey, region, locale) { //Some stuff self.region = region self. ...

What is the best method to assign each key in an Object to the corresponding value in another Object?

Suppose I have an object called data: { first: 'Zaaac', last: 'Ezzell', title: 'Mrs', mail: '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="83ece6f9f9e6efefb3c3f1e6e7e7eaf7ade ...