HTML Interview Questions and Answers for 5 Years Experience
Enjoy 35% off for first-time user! Join the Discord to claim your coupon!
We have digitized the content of this article and trained it into our AIHirely Interview Assistant. You can click the icon in the upper left corner to visit our product homepage. AIHirely is a real-time AI interview assistant that provides AI-generated reference answers to interviewers’ questions during live interviews. Additionally, you can use our AI Mock Interview feature for in-depth practice sessions tailored to your target job position and resume.
Question: What is the defer
attribute in the <script>
tag?
Answer:
The defer
attribute is a boolean attribute used in the <script>
tag in HTML. It is used to delay the execution of a script until the HTML document has been fully parsed and the DOM (Document Object Model) is completely constructed.
Key Points About the defer
Attribute:
-
Execution Order:
- When the
defer
attribute is used, the script is executed in the order it appears in the HTML document, but after the HTML parsing is complete. This means the script does not block the parsing of the HTML document, and it will run only after the document is fully loaded. - This ensures that the script will not block other resources from being fetched, making the page load faster.
- When the
-
When to Use:
- It is typically used when the script does not need to execute until the DOM is fully loaded, especially for scripts that manipulate the DOM (like jQuery or other libraries that need to interact with the page elements).
- It’s particularly useful when you want to load JavaScript at the end of the document but ensure it is executed after the page is parsed.
-
Compatible with External Scripts Only:
- The
defer
attribute only works with external scripts (i.e., when thesrc
attribute is present in the<script>
tag). It does not work for inline scripts.
- The
-
Async vs Defer:
async
: A script with theasync
attribute will be executed as soon as it is downloaded, without waiting for the document to finish parsing. This can lead to scripts running out of order.defer
: A script with thedefer
attribute will be executed after the document is fully parsed, and in the order it appears in the HTML.
-
Multiple
defer
Scripts:- If there are multiple
<script>
tags with thedefer
attribute, they will execute in the order they are written in the document after the HTML is parsed. This ensures the scripts do not interfere with each other.
- If there are multiple
Example Usage:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Defer Example</title>
<!-- Defer scripts until after HTML is fully parsed -->
<script src="script1.js" defer></script>
<script src="script2.js" defer></script>
</head>
<body>
<h1>Welcome to the Page!</h1>
<p>This is an example of using the defer attribute in scripts.</p>
</body>
</html>
In this example:
script1.js
andscript2.js
will be loaded asynchronously (without blocking the HTML parsing) but will execute only after the page is fully parsed.- They will execute in the order they appear in the HTML, i.e.,
script1.js
will execute beforescript2.js
.
Advantages of Using defer
:
- Improved Page Load Performance: By deferring the execution of JavaScript, the HTML document can be parsed without interruption, leading to faster page rendering.
- Non-blocking Execution: Scripts are loaded in the background, meaning they do not block the browser from rendering the HTML.
- Execution After DOM is Ready: Ensures that scripts that interact with the DOM are executed only after it is fully constructed, preventing errors related to accessing DOM elements before they are available.
In Summary:
The defer
attribute in the <script>
tag allows for scripts to be downloaded asynchronously, but executed only after the HTML document has been completely parsed. It helps optimize performance by preventing blocking during HTML rendering and ensures that DOM-dependent scripts run at the right time.
Question: What is the action
attribute in the <form>
tag?
Answer:
The action
attribute in the <form>
tag specifies the URL where the form data should be sent when the form is submitted. It defines the server endpoint that will process the submitted form data.
Key Points About the action
Attribute:
-
Specifies the Target URL:
- The
action
attribute contains the URL (web address) where the form data should be sent when the user submits the form. This URL can point to a file or script on the server (such as a PHP, Python, or Node.js file) that will handle the form data.
- The
-
Default Behavior:
- If the
action
attribute is omitted (i.e., it is left empty or not specified), the form will be submitted to the same URL as the page that contains the form. This is equivalent to settingaction="current-page"
.
- If the
-
Method and Action Together:
- The
action
attribute works with themethod
attribute, which defines the HTTP method (GET or POST) used to send the form data.GET
: Sends form data as part of the URL (in the query string).POST
: Sends form data as part of the HTTP request body, keeping the data hidden from the URL.
- The
-
Relative or Absolute URLs:
- The URL specified in the
action
attribute can be either:- Relative URL: A path relative to the current web page (e.g.,
action="/submit"
). - Absolute URL: A complete URL, including the protocol and domain (e.g.,
action="https://www.example.com/submit"
).
- Relative URL: A path relative to the current web page (e.g.,
- The URL specified in the
-
Handling Form Data:
- When the form is submitted, the browser sends the form data to the URL specified in the
action
attribute, and the server at that URL processes the data accordingly (e.g., saving to a database, sending an email, etc.).
- When the form is submitted, the browser sends the form data to the URL specified in the
Example Usage:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Form Example</title>
</head>
<body>
<!-- Form with action attribute -->
<form action="/submit-form" method="POST">
<label for="name">Name:</label>
<input type="text" id="name" name="name">
<button type="submit">Submit</button>
</form>
</body>
</html>
In this example:
- The form data will be sent to
/submit-form
(relative URL) using thePOST
method when the user clicks the submit button. - The server-side script at
/submit-form
will handle the form data.
Use Cases for the action
Attribute:
- Handling Form Submission on a Different Page:
- You can specify a different URL where the form data should be processed, such as a different script or server endpoint that processes the data and returns a response.
- Redirecting to a Confirmation Page:
- After a form is submitted, you may want the user to be redirected to a thank-you or confirmation page. This can be done by specifying the URL of the confirmation page in the
action
attribute.
- After a form is submitted, you may want the user to be redirected to a thank-you or confirmation page. This can be done by specifying the URL of the confirmation page in the
In Summary:
The action
attribute in the <form>
tag defines the URL where the form data is sent upon submission. It specifies the endpoint on the server that will process the form data, and it works in conjunction with the method
attribute (e.g., GET
or POST
) to determine how the data is transmitted.
Read More
If you can’t get enough from this article, Aihirely has plenty more related information, such as HTML interview questions, HTML interview experiences, and details about various HTML job positions. Click here to check it out.