Activity 1 - Javascript Basic.
Activity 1 - Javascript Basic.
13DDT22F1095
Learning Outcomes
By the end of this laboratory session, you should be able to:
1. Create a simple Javascript.
2. Embed Javascript in HTML page.
3. Create Javascript program using variables.
4. Apply operators in Javascript.
Hardware/ Software: HTML Editor (e.g.: Notepad/Notepad++/Eclipse/Adobe Dreamweaver/
Visual Studio)
Note:
JavaScript is one of the 3 languages all web developers must learn:
1. HTfillL to define the content of web pages
2. CSS to specify the layout of web pages
3. JavaScriR• to program the behavior of web pages
document.getElenert8yId(”c-n: !.st;•le.oispla; = ” :• ;
This activity shows how JavaScript can change the HTML content.
Step 1: Open a HTML editor file and type the following. Save code as act13a.html.
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript in Bcdy</h2>
Step 3: On act13a.html, modify the/avascrfpt code by remove the language and type.
<!DOCTYPE html>
<html><body>
<h2>JavaScript in Bcdy</h2>
<script>
document.getElementById(”demo").innerHTML = "My First JavaScript";
</scripts
</bcdy></html>
Step 4: Preview act13a.html again. Does it show the same output?
In HTML, JavaScript code must be inserted between <sckipt> and </sczipt> tags. Codes in
the first of act13a.html use the old version of JavaScript which use type and language attribute
as below:
<script language=”javascript" type="text/javascript">
But the use type and language attribute are not compulsory because JavaScript is the default
scripting language in HTML.
Step 1: Open a HTML editor file and type the following. Save code as act13a(ii).html.
<buttcn
onclick="document.getElementById('myImage').src='pic bulboff.gif'">
Turn cff the lights/buttons
</bodys </html>
Step 3: Preview act13a(ii).html in the browser. State or draw your observation. Which HTML
Tag attribute has been change by the JavaSript ?
Activity 13A (iii)
Activity Outcome: Change the HTML Style (CSS) using JavaScript
Step 1: Open a HTML editor file and type the following. Save code as act13a(iii).html.
<!DOCTYPE html><html><body>
<button type="button"
cnclick="document.getElementById('demc').style.fontSize='35px'”>Cli
ck Me!</buttons
</body></html>
Step 3: Write code to create button and reset the font size to the original size(12px)
4
Activity 13A (iv)
Activity Outcome: Show and Hide the HTML element using JavaScript
Step 1: Open a HTML editor file and type the following. Save code as act13a(iv).html.
<!DOCTYPE html><html><body>
</bodys </html>
Step 3: Open a HTML editor file and type the following. Save code as act13a(iv).html.
<!DOCTYPE html><html><body>
</body></html>
5
Activity 13B
Activity Outcome: Embed Javascript in HTML page.
You can place any number of scripts in an HTML document. Scripts can be
placed in the <body>, or in the <head> section of an HTML page, or in both. In this activity, a
JavaScript function is placed in the <head> section of an HTML page. The function is invoked
(called) when a button is clicked.
Step 1: Open a HTML editor file and type the following. Save code as act13b(i).html.
<!DOcTYPE html><html>
<head>
<script>
function myFunction() {
document.getElementById("demo").innerHTML "Paragraph
changed! ! !";
</script></heads
<bcdy>
<hl>A Web Pages/his
<p id="demc”>A Paragraphs/p>
<button type="button" onclick="myFunction()">Try it</button>
</body></html>
Step 3: On act13b(i).html, change the position of javascript and put it in <body> section and
save it as act13b(ii).html.
<!DOCTYPE html>
<html><body>
<h2>JavaScript in Bcdy</h2>
<p id="demo">A Paragraph.</p>
<button type="button” onclick="myFuncticn()">Try it</buttons
<script>
function myFunction() (
document.getElementById(”demo").innerHTML ”Paragraph
changed! ! !";
</scripts
</bcdy></html>
Step 4: Preview act13b(ii).html. Does it show the same output with lab activity act13b(i).html.
Step 5: JavaScript can also be placed in external files. On act13b(ii).html, change the code of
javascript and save it as act13b(iii).html.
<!DOCTYPE html>
<html><body>
<h2>External JavaScript</h2>
<p id="demo">A Paragraph.</p>
<button type=”button" onclick="myFunction()">Try it</buttons
<p>myFunction is stored in an external file called "myScript.js"</p>
<script src="myScript.js"></scripts
</bcdy></html>
Step 6: Open the new document, type the code below and save it as myScript.js.
function myFunction() {
document.getElementById(”demo").innerHTML ”Paragraph
changed! !!”;
Step 7: Preview act13b(iii).html. Does it show the same output with lab activity act13b(i).html
and lab activity act13b(ii).html.
7
JavaScript can be place either at <head> or <body>, find it also can be placed in external files.
External scripts are practical when the same code is used in many different web pages.
JavaScript files have the file extension ”.ys. To use an external script, put the name of the script file
in the src (source) attribute of a <sckipt> tag. The advantages of using external
JavaScript:
To add several script files to one page - use several script tags:
<script src="myScriptl.js”></scripts
<script src="myScript2.js”></scripts
External scripts can be referenced with a full URL or with a path relative to the current web
page. This example uses a full URL to link to a script:
<script src="https://www.w3schccls.com/js/myScript1.js"></scripts
8
Activity 13C(i)
Activity Outcome: Writing into an HTML element, using innerHTML
Step 1: Open a HTML editor file and type the following. Save code as
act13c(i).html.
<!DOcTYPE html>
<html><body>
<script>
document.getElementById("demo").innerHTML = 5 + G;
</scripts
</body></html>
Activity 13C(ii)
Activity Outcome: Writing into an HTML element, using document.write
Step 1: Open a HTML editor file and type the following. Save code as act13c(i).html.
<!DOcTYPE html>
<html><body>
<script>
document.write(5 + 6);
</scripts
</body></html>
Step 1: Open a HTML editor file and type the following. Save code as
act13c(iii).html.
<!DOCTYPE html>
<Iitml><bcdy>
<script>
windcw.alert(5 + 6);
</scripts
</body></html>
Activity 13C(iv)
Activity Outcome: Writing into an HTML element, using console.log()
Step 1: Open a HTML editor file and type the following. Save code as act13c(iv).html.
<!DOCTYPE html>
<Iitml><bcdy>
<script>
console.log(5 + 6);
</scripts
10
Activity 13D
Activity Outcome: Create Javascript program using variables.
JavaScript variables are containers for storing data values. In this lab activity, x, y, and z, are
variables:
Step 1: Open a HTML editor file and type the following. Save code as act13d(i).html.
<!DOCTYPE html>
<html><body>
<h2>JavaScript Variables</h2>
<p>In this example, x, y, and z are variables</p>
<script>
var x = 5;
var y = 6;
var z = x + y;
document.getElementById(”demo").innerHTML z;
</scripts
</bcdy></html>
<scrîpt>
</script>
</body></html>
JavaScript variables are containers for storing data values. In programming, just like in algebra,
we use variables (like price1) to hold values. In programming, just like in algebra, we use
variables in expressions (total = price1 + price2). From the activity above, you can calculate the
total to be 11. It’s a good programming practice to declare all variables at the beginning of a
script. You can declare many variables in one statement. Start the statement with var and
separate the variables by comma:
t2
Activity 13E
Activity Outcome: Apply operators in Javascript.
There are several type operators in JavaScript, but in this lab we will look at Arithmetic
operators. Arithmetic operators are used to perform arithmetic on numbers.
Operator Description
Addition
S ubLra ction
Multiplication
Di visio n
Modulus
++ Increment
Decrement
Step 1: Open a HTML editor file and type the following. Save code as act13e(i).html.
<!DOcTYPE html>
<html><body>
<h2>JavaScript Operators</h2>
<p>x = 5, y = 2, calculate z = x + y, and display z:</p>
<p id="demc"></p>
<script>
var x = 5;
var y = 2;
var z = x + y;
document.getElementById(”demo").innerHTML z;
</scripts
</body></html>
13
Step 3: The assignment operator (=) assigns a value to a variable. The addition operator (+)
adds numbers. Open a HTML editor file and type the following. Save code as act13d(ii).html.
<!DOCTYPE html>
<html><body>
<h2>The + Operators/h2>
<p id=”demo"></p>
<script>
var x = 5;
var y = 2;
var z = x + y;
document.getElementById("demo").innerHTML z;
</scripts
</bodv></html>
Step 4 : Preview act13e(ii).html in the browser. State or draw your observation.
Step 5: The multiplication operator (*) multiplies numbers. Open a HTML editor file and type
the following. Save code as act13d(iii).html.
<!DOCTYPE html>
<html><body>
<h2>The * Operators/h2>
<p id=”demo"></p>
<script>
var x = 5;
var y = 2;
var z = x * y;
document.getElementById("demo").innerHTML z;
</scripts
</body></html>
Step 7 You can try to use other JavaScript Arithmetic Operators and observe the output of
codes.
14