Learn about the HTML <script> tag and its usage in web development with JavaScript.
Explore how to include inline code, link to external files, and add interactivity to your web pages.
Get insights into best practices and examples.
HTML (Hypertext Markup Language) is the standard markup language for creating web pages. It provides the structure and content of a webpage by using various tags and elements. JavaScript, on the other hand, is a programming language that allows you to add interactivity and dynamic features to your web pages. It is often used in conjunction with HTML to enhance the functionality and behavior of a website.
alert("Hello, World!"); }
To create an HTML page with JavaScript, you can follow these steps:
1-Create a new HTML file using a text editor or an integrated development environment (IDE).
2-Save the file with a .html extension.
2-Start by defining the HTML structure using the <html>, <head>, and <body> tags.
3-The <head> section contains meta information and external resource references.
4- The <body> section contains the visible content of the webpage.
5-Within the <head> section, you can include a <script> tag to import your JavaScript code.
6-You have two Methods :
a)The first method:
You can embed the JavaScript directly within the <script> tag, or
b)The second method:
You can link to an external JavaScript file using the src attribute.
By Embedding JavaScript code within <script> tag:
1-Open text editor such as notepad te write the following code:
<!DOCTYPE html> <html> <head> <title>HTML with JavaScript</title> <script> // JavaScript code goes here function greet() {
</script> </head> <body> <h1>Welcome</h1> <button onclick="greet()">Click me</button> </body> </html>
2- from File menu save the file with nam :[ test.html] or as you like
3- by any web brwoswer open the test.html file to see :
The second method by linking the html file to an external JavaScript file:
1-open the textEditor the write the followin code:
<!DOCTYPE html> <html> <head> <title>HTML with JavaScript</title> <script src="script.js"></script> </head> <body> <h1>Welcome</h1> <button onclick="greet()">Click me</button> </body> </html>
2- create javscript file :
Open notepad the write the javascript code:
// External file of JavaScript code
function greet() { alert("Hello, World!"); }
3- by any web browser open the html file to see :
Explanation:
1-In both examples, we define a JavaScript function called greet() that displays an alert box with the message “Hello, World!” when the button is clicked.
2-The onclick attribute in the <button> tag is used to associate the function with the button’s click event.
Uses of JavaScript in HTML page:
JavaScript can be used to perform various tasks on a webpage, such as :
1-form validation
2-DOM manipulation (changing the content or style of HTML elements dynamically)
3-making AJAX requests to fetch data from a server, and much more.
Its versatility and ability to interact with the HTML elements make it an essential tool for creating interactive and dynamic web pages.
Note: Remember to place the JavaScript code either within the <script> tags or in a separate file with the .js extension and link it using the src attribute in the <script> tag.
The HTML <script> tag is used to embed or reference JavaScript code within an HTML document. It allows you to include JavaScript code directly within the HTML file or link to an external JavaScript file.
Here are the different ways that the <script> tag can be used:
You can embed JavaScript code directly within the <script> tags in the HTML file.
This is done by placing the JavaScript code between the opening <script> tag and the closing </script> tag.
For example:
<!DOCTYPE html> <html> <head> <title>HTML with JavaScript</title> <script> // JavaScript code goes here function greet() { alert("javascript code inside the html code !"); } </script> </head> <body> <h1>Welcome</h1> <button onclick="greet()">Click me</button> </body> </html>
This approach is useful for small code snippets or quick scripting tasks.
You can also link to an external JavaScript file using the src attribute of the <script> tag.
The src attribute specifies the path to the JavaScript file.
For example:
<script src="script.js"></script>
Explanation:
In this case, the JavaScript code is written in a separate file named “script.js”, and the <script> tag is used to reference and import that file into the HTML document.
This approach is suitable for larger JavaScript codebases or when you want to reuse the same JavaScript file across multiple HTML pages.
The <script> tag can be used to define event handlers directly within HTML elements using the on-event attributes.
For example:
<button onclick="alert('Button clicked!')">Click me</button>
Explanation:
In this case, the JavaScript code alert(‘Button clicked!’) is directly embedded within the onclick attribute of the <button> element. When the button is clicked, the JavaScript code will be executed.
It’s worth noting that the <script> tag is typically placed within the <head> or <body> section of the HTML document. Placing it in the <head> section allows the JavaScript code to be loaded and executed before the entire webpage is rendered, while placing it in the <body> section allows the HTML content to be loaded first, and then the JavaScript code is executed.
complete code example
Here’s a complete code example that demonstrates the usage of the <script> tag to include JavaScript code within an HTML document:
<!DOCTYPE html> <html> <head> <title>HTML with JavaScript</title> <script> // Inline JavaScript code function greet() { alert("Hello, World!"); } </script> </head> <body> <h1>Welcome</h1> <button onclick="greet()">Click me</button> </body> </html>
Explanation:
1-In this example, we have an HTML document that includes a <script> tag within the <head> section.
2-Inside the <script> tag, we define a JavaScript function called greet(). This function displays an alert box with the message “Hello, World!” when invoked.
3-In the <body> section, we have an <h1> element with the text “Welcome” and a <button> element.
4-The onclick attribute of the button is set to greet(), which means when the button is clicked, the greet() function will be called, triggering the alert dialog.
You can save the above code in an HTML file, such as index.html, and open it in a web browser to see the result. When you click the “Click me” button, an alert box will pop up with the message “Hello, World!”.
This example shows the basic usage of the <script> tag to include JavaScript code directly within an HTML file.
Note:
Remember to place the JavaScript code between the opening and closing <script> tags or link to an external JavaScript file using the src attribute within the <script> tag.
Here’s a multiple-choice quiz based on the lesson content. Each question is followed by four options (A, B, C, D), and the correct answer is indicated with an asterisk (*) after the option.
1-What does HTML stand for?
*Answer: A
2-What is the purpose of JavaScript in web development?
*Answer: B
3-Which HTML tag is used to include JavaScript code within an HTML file?
*Answer: C
4-How can you embed JavaScript code within an HTML file?
*Answer: A
5-What is the purpose of the src attribute in the <script> tag?
*Answer: D
6-Which of the following examples demonstrates linking to an external JavaScript file?
A)
<script>
function greet() {
alert(“Hello, World!”);
}
</script>
B)
<script src=”script.js”></script>
C)
<script>
src=”script.js”
</script>
D)
<script href=”script.js”></script>
*Answer: B
7-Where should you typically place the <script> tag within an HTML document?
*Answer: A
8-What can JavaScript be used for in web development?
*Answer: D
9-Which of the following is true about inline JavaScript code?
*Answer: D
10-What is the purpose of the defer attribute in the <script> tag?
*Answer: B
11-Which attribute is used to specify an alternative script for browsers that do not support the <script> tag?
*Answer: D
12-What happens if the async attribute is added to the <script> tag?
*Answer: B
13-How can you use the <noscript> tag in combination with the <script> tag?
*Answer: A
14-Which of the following is true about the placement of the <script> tag in an HTML file?
*Answer: B
15-How can you specify the MIME type of a script included using the <script> tag?
*Answer: A
16-What is the purpose of the integrity attribute in the <script> tag?
*Answer: B
17-How can you include an external JavaScript file using the <script> tag and ensure it is loaded asynchronously?
*Answer: B
18-What is the purpose of the document.write() method in JavaScript?
*Answer: C
19-Remember to review the answers and explanations to ensure you understand each question thoroughly.
MDN Web Docs – <script>: The official Mozilla Developer Network (MDN) documentation provides a comprehensive guide on using the <script> tag in HTML. It covers various attributes, placement options, and best practices.
Reference: MDN Web Docs – <script>
MDN Web Docs – JavaScript: MDN also provides extensive documentation on JavaScript, including language fundamentals, syntax, and usage in web development.