Hope you did well on your exam!

If this site helped, please leave feedback, check out the project on GitHub, or follow me on GitHub.

F5: JavaScript 1 code examples

Code examples from the course Google Drive for the JavaScript 1 lecture. View the source and run live previews where available.

Back
My First Web Page โ€” document.write

5.html

HTML18 lines
<!DOCTYPE html>
<html>
<body>
<h2>My First Web Page</h2>
<p>My first paragraph.</p>
<p>What will displayed in this page?</p>
<script>
document.write("5" + 6);
console.log("Hello");
</script>
</body>
</html>

My First Web Page โ€” window.alert

7.html

HTML15 lines
<!DOCTYPE html>
<html>
<body>
<h2>My First Web Page</h2>
<p>My first paragraph.</p>
<script>
window.alert(5 + 6);
</script>
</body>
</html>

External JavaScript file

00.html

HTML14 lines
<!DOCTYPE html>
<html>
<head>
<script src="myjs.js"></script>
</head>
<body>
<h1>This is the beginning of the body</h1>
<script>
document.write("<h1>Hello World</h1>");
document.write("A new line");
</script>
<h3>This is after the scripts</h3>
</body>
</html>

My First JavaScript โ€” display date on click

1.html

HTML17 lines
<!DOCTYPE html>
<html>
<body>
<h2>My First JavaScript</h2>
<button type="button" onclick="document.getElementById('demo').innerHTML = Date();document.getElementById('demo2').innerHTML ='This is after the date';">
Click me to display Date and Time.
</button>
<p id="demo"></p>
<div id="demo2"></div>
</body>
</html>

What Can JavaScript Do? โ€” change image src

2.html

HTML18 lines
<!DOCTYPE html>
<html>
<body>
<h2>What Can JavaScript Do?</h2>
<!--JavaScript can change HTML attribute values -->
<button onclick="document.getElementById('myImage').src='pic_bulbon.gif';">Turn on the light</button>
<img id="myImage" src="pic_bulboff.gif" style="width:100px">
<button onclick="document.getElementById('myImage').src='pic_bulboff.gif'">Turn off the light</button>
</body>
</html>

What Can JavaScript Do? โ€” change element style

3.html

HTML22 lines
<!DOCTYPE html>
<html>
<head>
<script>
function myfunction(buttonId){
document.getElementById('demo').style.fontSize='35px';
alert(document.getElementById(buttonId).innerText);
}
</script>
</head>
<body>
<h2>What Can JavaScript Do?</h2>
<p id="demo">JavaScript can change the style of an HTML element.</p>
<button id="b1" type="button" onclick="myfunction('b1');">1</button>
</body>
</html>

What Can JavaScript Do? โ€” hide HTML elements

4.html

HTML14 lines
<!DOCTYPE html>
<html>
<body>
<h2>What Can JavaScript Do?</h2>
<p id="demo">JavaScript can hide HTML elements.</p>
<button type="button" onclick="document.getElementById('demo').style.display='none'">Click Me!</button>
</body>
</html>

What Can JavaScript Do? โ€” show hidden elements

6.html

HTML16 lines
<!DOCTYPE html>
<html>
<body>
<h2>What Can JavaScript Do?</h2>
<p>JavaScript can show hidden HTML elements.</p>
<p id="demo" style="display:none">Hello JavaScript!</p>
<button type="button" onclick="document.getElementById('demo').style.display='block'">Click Me!</button>
</body>
</html>

Global variable

8.html

HTML28 lines
<!DOCTYPE html>
<html>
<head>
<script>
//global vairable
var myvariable = 5;
console.log(window.myvariable);
console.log(frames.myvariable);
console.log(self.myvariable);
console.log(this.myvariable);
console.log(globalThis.myvariable);
function myfunction(){
var myvariable=9;
alert(myvariable);
}
myfunction();
</script>
</head>
<body>
<h2>Global vairable</h2>
<p><script>document.writeln(window.myvariable);</script>Test in console</p>
</body>
</html>

Objects

9.html

HTML21 lines
<!DOCTYPE html>
<html>
<head>
<script>
var employee ={
1:"Patrick",
2: "Morin",
eId:24433433
};
document.write(employee[1] + ", " + employee[2] + ", " + employee.eId);
</script>
</head>
<body>
<h2>objects</h2>
</body>
</html>

Variable scope

000.html

HTML22 lines
<!DOCTYPE html>
<html>
<head>
<script>
function myfunction(){
if(1){var z=3;}
}
try {
console.log(z);
} catch (error) {
document.writeln(error.name + ": " + error.message);
}
</script>
</head>
<body>
</body>
</html>

My First JavaScript โ€” alert, confirm, and prompt

0.html

HTML26 lines
<!DOCTYPE html>
<html>
<head>
<script>
var myvalue=60;
function showmsg() {
//alert("This is an alert");
//confirm("This is a confirmation message");
var myvalue = window.prompt("Enter your age","20");
document.writeln(myvalue);
}
</script>
</head>
<body>
<h2>My First JavaScript</h2>
<script>
functionvalue = showmsg();
document.writeln(myvalue);
</script>
<p id="demo"></p>
</body>
</html>