Lecture 14 Java Script Part 1
Lecture 14 Java Script Part 1
Module 2
Content
• Introduction
• What can we do?
• Where to write?
• Generate output
Introduction
• JavaScript is the world's most popular programming language.
• JavaScript is the programming language of the Web.
• JavaScript is easy to learn.
</body>
</html>
What JavaScript can do? Change HTML Attribute Values
<!DOCTYPE html>
<html>
<body>
</body>
</html>
What JavaScript Change HTML Style (CSS)
can do?
<!DOCTYPE html>
<html>
<body>
</body>
</html>
What JavaScript Hide HTML Elements
can do?
<!DOCTYPE html>
<html>
<body>
</body>
</html>
What JavaScript Show HTML Elements
can do?
<!DOCTYPE html>
<html>
<body>
</body>
</html>
Where to write JavaScript code?
<!DOCTYPE html>
<html> The <script> Tag
<body>
<h2>JavaScript in Body</h2>
• In HTML, JavaScript code is inserted between
<script>
A JavaScript function
function myFunction() {
• It is a block of JavaScript code, that can be
document.getElementById("demo").innerHTML = "Paragraph
changed."; executed when "called" for.
}
</script> • Example: a function can be called when
</head> an event occurs, like when the user
<body>
clicks a button.
<h2>JavaScript in Head</h2>
</body>
</html>
Where to write JavaScript code?
A JavaScript function
<!DOCTYPE html>
<html> • It is a block of JavaScript code, that can be
<body>
executed when "called" for.
<h2>JavaScript in Body</h2>
• Example: a function can be called when
<p id="demo">A Paragraph.</p>
an event occurs, like when the user clicks
<button type="button" onclick="myFunction()">Try it</button>
a button.
<script>
function myFunction() {
document.getElementById("demo").innerHTML = "Paragraph
changed."; Script is written in the body
}
</script>
</body>
</html>
Where to write JavaScript code? External JavaScript
<p>This example links to "myScript.js".</p> script file in the src (source) attribute of a <script>
<p>(myFunction is stored in "myScript.js")</p>
tag
<script src="myScript.js"></script>
<h2>External JavaScript</h2>
<script src="myScript.js"></script>
</body>
</html>
function myFunction() {
document.getElementById("demo").innerHTML = "Paragraph
changed.";
}
myScript.js
Where to write JavaScript code?
External JavaScript
Placing scripts in external files has some advantages:
To add several script files to one page - use several script tags:
<script src="myScript1.js"></script>
<script src="myScript2.js"></script>
Where to write JavaScript code?
External References
• Example:
<script src="https://www.w3schools.com/js/myScript.js"></scrip
t>
content
JavaScript Output
<!DOCTYPE html>
<html>
Document.write()
<body>
</body>
is loaded, will delete all existing HTML
</html>
JavaScript Output Document.write()
<!DOCTYPE html>
<html> • For testing purposes, it is convenient to
<body>
use document.write()
<h1>My First Web Page</h1>
<p>My first paragraph.</p>
• Using document.write() after an HTML
<button type="button" onclick="document.write(5 + 6)">Try
it</button>
document is loaded, will delete all
</body>
</html> existing HTML
windows.alert()
JavaScript Output • We can use an alert box to display data
<!DOCTYPE html> • We can skip the window keyword.
<html>
<body> • In JavaScript, the window object is the global
scope object,
<h1>My First Web Page</h1>
<p>My first paragraph.</p> • It means that variables, properties, and methods
<script>
console.log(5 + 6);
</script>
</body>
</html>
JavaScript Output
<!DOCTYPE html> windows.print()
<html>
<body> • JavaScript does not have any print object or
print methods.
<h2>The window.print() Method</h2>
• You cannot access output devices from
<p>Click the button to print the current page.</p>
JavaScript.
<button onclick="window.print()">Print this
• The only exception is that you can call the
page</button>
window.print() method in the browser to print
</body>
</html> the content of the current window.
JavaScript Output
<!DOCTYPE html>
<html> windows.print()
<body> • JavaScript does not have any print object or
<h2>The window.print() Method</h2> print methods.
<p>Click the button to print the current page.</p> • You cannot access output devices from
JavaScript.
<button onclick="window.print()">Print this
page</button> • The only exception is that you can call the