JavaScript/C2/How-to--use-JS-in-HTML/English
Title of the script: How to use JS in HTML
Author: Jayesh Katta Ramalingaiah
Domain Reviewer:
Novice Reviewer:
Keywords: JavaScript, HTML, script, body
|
|
Slide: Title | Hello and Welcome to the spoken tutorial on “How to use Javascript in HTML”. |
Slide: Learning Objectives | In this tutorial, we will learn about:
|
Slide: System Specifications | This tutorial is recorded using:
However you may use any other browser of your choice. |
Slide : Pre-requisites | To practice this tutorial,
|
Slide: Code files |
|
Slide: Approaches to use JS with HTML | There are mainly two approaches to use JS with HTML.
|
Slide: Embedding JS in HTML
|
|
Only Narration | Adding script tag after the last element in the body tag is a preferred way but not the only way.
|
Point to the Practice-JS folder on Desktop | Let’s create a folder named Practice-JS on our Desktop. |
Open Visual Studio Code editor. | I will use Visual Studio Code editor for this demonstration. |
Slide IDE - Extension |
|
Switch to Editor | Switch back to the editor. |
[Editor]
Open Folder -> Practice-JS |
In the Welcome page, click on the Open folder link at the top left corner.
|
[Editor] Click on the Extension icon on the left | Click on the Extensions icon in the left side activity bar. |
[Editor] [Extension Tab]:
Type: live server |
In the search bar, type “Live Server”.
|
Only Narration | The installation may take some time depending upon your internet speed. |
Close and reopen Visual Code Studio | After installation, restart the editor by closing and opening it again. |
[Editor]:
Click on add file icon |
Go to the Explorer panel on the left hand side.
|
[Editor] Type index.html | Type index.html as the file name.
|
[Editor] Type:
<html lang="en"> <head> <title>How to use JS in HTML</title> </head> <body> <h1>Welcome to Spoken Tutorials</h1> <script> if (true) { console.log("I'm inside if!"); } else { console.log("I'm inside else!"); } </script> </body> </html> |
In the index.html file, type the code as shown.
|
Press: Ctrl + S | Save the file. |
Press: Alt + L and Alt + O
|
Now press the key combinations Alt + L and Alt + O.
|
Show firefox | The default browser will open automatically and a new tab opens.
|
[Firefox] Point to Welcome to Spoken Tutorials | Welcome to Spoken Tutorials is displayed. |
[Firefox] Press Ctrl + Shift + I
|
Now press Ctrl + Shift + I keys together.
|
[Firefox] Click on the Console tab | Now click on the console tab in the developer tools.
|
[Firefox] Highlight
I'm inside if! |
You can see I'm inside if! printed in the console.
|
Switch to Editor | Switch back to the editor. |
[Editor] Highlight:
<script> </script> |
Here we have used a script tag to embed JS code in an HTML file.
|
[Editor] Highlight:
console.log("I'm inside if!"); } else { console.log("I'm inside else!"); } |
We have written a simple if else condition, where the if condition is always true and the if block gets executed. |
[Editor] Highlight:
|
The console.log statement is used to print the message on the console.
|
Only narration | Next let us learn another approach.
|
Slide: Importing External JS File |
|
Switch to Editor | Switch to the editor. |
[Editor]
Click on add file icon |
In the Explorer panel on the left hand side, click on the New file icon beside the Practice-JS folder name.
|
[Editor] Type main.js | Type main.js as the file name and press Enter
|
[Editor] Type:
console.log("I'm inside if!"); } else { console.log("I'm inside else!"); } |
In the main.js file, type the code as shown.
|
Press: Ctrl + S | Save the file. |
[Editor] Type:
<html lang="en"> <head> <title>How to use JS in HTML</title> </head> <body> <h1>Welcome to Spoken Tutorials</h1> <script src="main.js"></script> </body> </html> |
Switch to the index.html file and replace the code as shown here.
|
Press: Ctrl + S | Save the file. |
Switch to Browser | Switch back to the browser. |
[Browser] [Console Tab]
|
Now, we can see the output, I'm inside if! |
Switch to Editor | Switch back to the editor. |
[Editor] Highlight:
|
Here we have used a script tag, with property src which takes the path to the JS file.
|
[Editor] Highlight:
console.log("I'm inside if!"); } else { console.log("I'm inside else!"); } |
In the main.js file, we have written the same if else condition.
|
[Editor] Highlight:
console.log("I'm inside if!"); |
The console.log statement is used to print the message on the console. |
Only Narration | Now, let’s conclude which is the better approach. |
Slide: Embedded approach |
|
Slide: Importing External JS File | Whereas, in the second approach,
|
Only narration | With this we have come to the end of this tutorial.
|
Slide: Summary | In this tutorial, we have learnt:
|
Slide: Assignment | As an assignment,
|
Slide: About Spoken Tutorial Project |
|
Slide: Spoken tutorial workshops |
|
Slide: Answers for THIS Spoken Tutorial
|
|
Slide : Forum for specific questions |
|
Slide: Acknowledgement | Spoken Tutorial Project is funded by Ministry of Education (MoE), Government of India. |
Slide: Thanks | The script for this tutorial is contributed by Jayesh.
And this is Praveen from IIT Bombay signing off. Thank you for joining. |