Chapter 5 - Introduction to Javascript
Chapter 5 - Introduction to Javascript
Development &
Application
Introduction to JavaScript
JavaScript is the scripting language of the Web.
What JavaScript do?
JavaScript Can Change HTML Content:
document.getElementById("demo").innerHTML = "Hello JavaScript";
document.getElementById('demo').innerHTML = 'Hello JavaScript’;
<button
onclick="document.getElementById('myImage').src='pic_bulbon.gif’”>
Turn on the light
</button>
<button
turn on the light turn off the light onclick="document.getElementById('myImage').src='pic_bulboff.gif’”>
Turn off the light
</button>
What JavaScript can do?
JavaScript Can Change HTML Styles (CSS)
document.getElementById("demo").style.fontSize = "35px";
<!DOCTYPE html>
<html>
<head>
<script>
function myFunction() {
document.getElementById("demo").innerHTML = "Paragraph changed.";
}
</script>
</head>
<body>
<h2>Demo JavaScript in Head</h2>
<p id="demo">A Paragraph</p>
<button type="button" onclick="myFunction()">Try it</button> Try it
</body>
</html>
RESET
JavaScript Where to?
JavaScript in <body>
<!DOCTYPE html>
<html>
<body> Try it
<h2>Demo JavaScript in Body</h2>
<p id="demo">A Paragraph.</p>
<button type="button" onclick="myFunction()">Try it</button>
<script>
function myFunction() {
document.getElementById("demo").innerHTML = "Paragraph changed.";
}
</script>
</body>
</html>
RESET
JavaScript Where to?
JavaScript in <body>
<!DOCTYPE html>
<html>
<body> Try it
<h2>Demo JavaScript in Body</h2>
<p id="demo">A Paragraph.</p>
<button type="button" onclick="myFunction()">Try it</button>
<script>
function myFunction() {
document.getElementById("demo").innerHTML = "Paragraph changed.";
}
</script>
</body>
</html>
RESET
JavaScript Where to?
External JavaScript
External scripts are practical when the same code is used in many different web pages.
To use an external script, put the name of the script file in the src (source) attribute
of a <script> tag:
<script src="myScript.js"></script>
You can place an external script reference in <head> or <body> as you like.
The script will behave as if it was located exactly where the <script> tag is located.
JavaScript Output
JavaScript Display Possibilities
<!DOCTYPE html>
<html>
<body>
<h1>My First Web Page</h1>
<p>My First Paragraph</p>
<p id="demo"> </p>
<script>
document.getElementById("demo").innerHTML = 5 + 6;
</script>
</body>
</html>
JavaScript Output
Using document.write()
Try it
RESET
JavaScript Output
Using window.alert()
<!DOCTYPE html>
<html>
<body>
<h1>My First Web Page</h1> OK
<p>My first paragraph.</p>
<script>
window.alert(5 + 6);
//can use alert(5 + 6) without window.
</script>
</body>
</html>
RESET
JavaScript Output
Using console.log()
For debugging purposes, you can call the console.log() method in the browser to display
data.
<!DOCTYPE html>
<html>
<body>
<script>
console.log(5 + 6);
</script>
</body>
</html>
JavaScript Output
JavaScript Print
For debugging purposes, you can call the console.log() method in the browser to display
data.
<!DOCTYPE html>
<html>
<body>
</body>
</html>