Difference between revisions of "JavaScript/C2/How-to--use-JS-in-HTML/English"
(Created page with "Title of the script: How to use JS in HTML Author: Jayesh Katta Ramalingaiah Domain Reviewer: Novice Reviewer: Keywords: JavaScript, HTML, script, body {| border =...") |
Nancyvarkey (Talk | contribs) |
||
(2 intermediate revisions by 2 users not shown) | |||
Line 3: | Line 3: | ||
Author: Jayesh Katta Ramalingaiah | Author: Jayesh Katta Ramalingaiah | ||
− | Domain Reviewer: | + | Domain Reviewer: Ankita Maske |
− | Novice Reviewer: | + | Novice Reviewer: Praveeen S. |
Keywords: JavaScript, HTML, script, body | Keywords: JavaScript, HTML, script, body | ||
Line 22: | Line 22: | ||
|| Slide: Learning Objectives | || Slide: Learning Objectives | ||
|| In this tutorial, we will learn about: | || In this tutorial, we will learn about: | ||
− | |||
− | |||
* The different approaches to use '''Javascript (JS)''' with '''HTML''' | * The different approaches to use '''Javascript (JS)''' with '''HTML''' | ||
* When to use each approach | * When to use each approach | ||
− | |||
− | |||
|- | |- | ||
|| Slide: System Specifications | || Slide: System Specifications | ||
|| This tutorial is recorded using: | || This tutorial is recorded using: | ||
− | |||
− | |||
* '''Ubuntu Linux''' OS version 18.04 | * '''Ubuntu Linux''' OS version 18.04 | ||
* '''Visual Studio Code''' version 1.51.1 ('''code editor''') | * '''Visual Studio Code''' version 1.51.1 ('''code editor''') | ||
Line 43: | Line 37: | ||
|| Slide : Pre-requisites | || Slide : Pre-requisites | ||
|| To practice this tutorial, | || To practice this tutorial, | ||
− | |||
− | |||
* You should have some basic knowledge on '''HTML''' | * You should have some basic knowledge on '''HTML''' | ||
* Basic understanding of concepts of any programming language | * Basic understanding of concepts of any programming language | ||
* If not then please go through the '''HTML''' and any '''programming language''' series on this website. | * If not then please go through the '''HTML''' and any '''programming language''' series on this website. | ||
− | + | * Also, you should have an internet connection to install the necessary packages | |
− | + | ||
|- | |- | ||
Line 55: | Line 46: | ||
|| | || | ||
* The files used in this tutorial are available in the''' Code files''' link on this tutorial page. | * The files used in this tutorial are available in the''' Code files''' link on this tutorial page. | ||
− | * Pls download and extract the | + | * Pls download and extract the files. |
* Make a copy and then use them for practicing. | * Make a copy and then use them for practicing. | ||
− | |||
− | |||
|- | |- | ||
|| Slide: Approaches to use JS with HTML | || Slide: Approaches to use JS with HTML | ||
|| There are mainly two approaches to use '''JS '''with '''HTML.''' | || There are mainly two approaches to use '''JS '''with '''HTML.''' | ||
− | |||
− | |||
* First approach is '''embedding JS '''in '''HTML''' | * First approach is '''embedding JS '''in '''HTML''' | ||
* Second approach is '''importing '''an external '''JS '''file. | * Second approach is '''importing '''an external '''JS '''file. | ||
− | |||
− | |||
|- | |- | ||
|| Slide: Embedding JS in HTML | || Slide: Embedding JS in HTML | ||
− | |||
− | |||
− | |||
|| | || | ||
* We need to write '''JS '''code in between '''script tags''' in the '''HTML '''file. | * We need to write '''JS '''code in between '''script tags''' in the '''HTML '''file. | ||
* The '''script tag''' should be written after the last '''HTML element''' in the '''body tag'''. | * The '''script tag''' should be written after the last '''HTML element''' in the '''body tag'''. | ||
− | |||
− | |||
|- | |- | ||
Line 103: | Line 83: | ||
* This would help us in '''hot reload.''' | * This would help us in '''hot reload.''' | ||
* This means that any code change is automatically reflected on the '''browser''' when we save the file. | * This means that any code change is automatically reflected on the '''browser''' when we save the file. | ||
− | |||
− | |||
|- | |- | ||
Line 125: | Line 103: | ||
− | Then click on the ''' | + | Then click on the '''OK''' button at the top right corner. |
|- | |- | ||
Line 138: | Line 116: | ||
− | Click on the '''Install '''button adjacent to | + | Click on the '''Install '''button adjacent to '''Live Server''' extension. |
|- | |- | ||
Line 170: | Line 148: | ||
This is because the '''server renders''' the '''index page''' by default. | This is because the '''server renders''' the '''index page''' by default. | ||
+ | Press '''Enter'''. | ||
The file '''index.html '''opens. | The file '''index.html '''opens. | ||
Line 263: | Line 242: | ||
|| <nowiki>[Firefox] Click on the </nowiki>'''Console''' tab | || <nowiki>[Firefox] Click on the </nowiki>'''Console''' tab | ||
|| Now click on the '''console tab''' in the '''developer tools.''' | || Now click on the '''console tab''' in the '''developer tools.''' | ||
− | |||
− | |||
− | |||
|- | |- | ||
Line 322: | Line 298: | ||
|| Only narration | || Only narration | ||
|| Next let us learn another approach. | || Next let us learn another approach. | ||
− | |||
− | |||
− | |||
|- | |- | ||
Line 331: | Line 304: | ||
* We will create a new '''JS''' file, and write '''JS''' code in the file | * We will create a new '''JS''' file, and write '''JS''' code in the file | ||
* Then '''link''' the created '''JS''' file with '''HTML''' using '''script''' '''src''' '''tag'''. | * Then '''link''' the created '''JS''' file with '''HTML''' using '''script''' '''src''' '''tag'''. | ||
− | |||
− | |||
|- | |- | ||
Line 472: | Line 443: | ||
* In the '''embedded''' approach, we have written '''JS''' code in an '''HTML''' file using '''script''' '''tag''' | * In the '''embedded''' approach, we have written '''JS''' code in an '''HTML''' file using '''script''' '''tag''' | ||
* This code is only used for that particular '''HTML''' file and cannot be reused in any other '''HTML''' file | * This code is only used for that particular '''HTML''' file and cannot be reused in any other '''HTML''' file | ||
− | |||
− | |||
|- | |- | ||
|| Slide: Importing External '''JS '''File | || Slide: Importing External '''JS '''File | ||
|| Whereas, in the second approach, | || Whereas, in the second approach, | ||
− | |||
− | |||
* We have written '''JS''' code by creating an external file and linking it to the '''HTML''' file. | * We have written '''JS''' code by creating an external file and linking it to the '''HTML''' file. | ||
* Using this approach, we can use the same '''JS''' code in different '''HTML''' files by just '''linking''' it. | * Using this approach, we can use the same '''JS''' code in different '''HTML''' files by just '''linking''' it. | ||
* This solves the problem of writing the same code in different files so we can reuse the same. | * This solves the problem of writing the same code in different files so we can reuse the same. | ||
− | |||
− | |||
|- | |- | ||
Line 496: | Line 461: | ||
|| Slide''': '''Summary''' ''' | || Slide''': '''Summary''' ''' | ||
|| In this tutorial, we have learnt: | || In this tutorial, we have learnt: | ||
− | |||
− | |||
* Different approaches to use '''JS''' with '''HTML''' | * Different approaches to use '''JS''' with '''HTML''' | ||
* And their implications in coding | * And their implications in coding | ||
− | |||
− | |||
|- | |- | ||
|| Slide: Assignment | || Slide: Assignment | ||
|| As an assignment, | || As an assignment, | ||
− | |||
− | |||
* Create a file named '''assignment.js''' | * Create a file named '''assignment.js''' | ||
* Log '''A''' and '''B '''characters | * Log '''A''' and '''B '''characters | ||
Line 513: | Line 472: | ||
* '''Link''' the '''assignment.js '''file to it | * '''Link''' the '''assignment.js '''file to it | ||
* Observe the output in the '''browser’s console'''. | * Observe the output in the '''browser’s console'''. | ||
− | |||
− | |||
|- | |- | ||
Line 521: | Line 478: | ||
* The video at the following link summarises the Spoken Tutorial project. | * The video at the following link summarises the Spoken Tutorial project. | ||
* Please download and watch it | * Please download and watch it | ||
− | |||
− | |||
|- | |- | ||
Line 529: | Line 484: | ||
* We conduct workshops using spoken tutorials and give certificates. | * We conduct workshops using spoken tutorials and give certificates. | ||
* For more details, please write to us. | * For more details, please write to us. | ||
− | |||
− | |||
|- | |- | ||
|| Slide: Answers for THIS Spoken Tutorial | || Slide: Answers for THIS Spoken Tutorial | ||
− | |||
− | |||
− | |||
|| | || | ||
* Do you have questions in THIS Spoken Tutorial? | * Do you have questions in THIS Spoken Tutorial? | ||
Line 544: | Line 494: | ||
* The Spoken Tutorial project will ensure an answer. | * The Spoken Tutorial project will ensure an answer. | ||
* You will have to register to ask questions. | * You will have to register to ask questions. | ||
− | |||
− | |||
|- | |- | ||
Line 554: | Line 502: | ||
* This will help reduce the clutter. | * This will help reduce the clutter. | ||
* With less clutter, we can use these discussions as instructional material. | * With less clutter, we can use these discussions as instructional material. | ||
− | |||
− | |||
|- | |- |
Latest revision as of 11:10, 21 June 2021
Title of the script: How to use JS in HTML
Author: Jayesh Katta Ramalingaiah
Domain Reviewer: Ankita Maske
Novice Reviewer: Praveeen S.
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.
Press Enter. The file index.html opens. |
[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. |