Professional Documents
Culture Documents
JS1
JS1
JS1
I. Introduction
JavaScript is the programming language of HTML and the Web. Programming makes computers
do what you want them to do. JavaScript is easy to learn.
Example:
sample1.html
<html>
<head>
<script src=”functionality.js”></script>
</head>
<body>
<h2>Change text using JavaScript.</h2>
<div id="demo">JavaScript can change HTML content</div>
<button type="button" id=”textChanger”>Click Me!</button>
</body>
</html>
functionality.js
window.onload = function() {
var txt = document.getElementById(“demo”);
var btn = document.getElementById(“textChanger”);
btn.onclick = function() {
txt.innerHTML =”Hello JavaScript”;
}
}
JavaScript A Programming language for Web development
functionality.js
window.onload = function() {
var bulb = document.getElementById("bulb");
var btnOff = document.getElementById("lightOff");
var btnOn = document.getElementById("lightOn");
btnOff.onclick = function() {
bulb.src = "..\\JavaScriptFiles\\src\\pic_bulboff.gif";
}
btnOn.onclick = function() {
bulb.src = "..\\JavaScriptFiles\\src\\pic_bulbon.gif";
}
}
style.css
#div1 {
text-align:center;
border:solid 2px;
}
#lightOff:hover {
background-color:rgba(247, 33, 50, 0.849);
}
JavaScript A Programming language for Web development
#lightOn:hover {
background-color:rgb(159, 226, 34);
}
.btn {
color:white;
height:30px;
width:90px;
border-radius:5px;
border:dotted;
}
functionality.js
window.onload = function() {
var x = 5;
if(x <= 5) {
document.getElementById("firstDiv").innerHTML = "x is greater than or equal to 5";
}
}
OR
functionality.js
window.onload = function() {
var x = 5;
if(x <= 5) {
document.getElementById("firstDiv").innerHTML = "x is less than or equal to 5";
}
else {
document.getElementById("firstDiv").innerHTML = "x is greater than 5";
}
}
JavaScript A Programming language for Web development
Try this!(30mins)
Using the sample2.html and its corresponding resources, make the bulb turn on/off using only 1
button.
V. SetInterval in JavaScript
The setInterval() method calls a function or evaluates an expression at specified intervals(milliseconds).
Example:
sample4.html
<html>
<head>
<script src="functionality.js"></script>
</head>
<body>
<p>A script on this page starts this clock:</p>
<p id="demo"></p>
</body>
</html>
functionality.js
var myVar = setInterval(myTimer, 1000);
function myTimer() {
var d = new Date();
var t = d.toLocaleTimeString();
document.getElementById("demo").innerHTML = t;
}
functionality.js
window.onload = function() {
var btn = document.getElementById("btn");
var w = window.innerWidth;
var h = window.innerHeight;
btn.onclick = function() {
document.getElementById("demo").innerHTML = "Width: " + w + "<br>Height: "+ h;
}
}