Javascript
Javascript
Java Script in
HTML Documents
page (84)
Learning Objectives
You will learn about:
► the importance of JavaScript
► writing a JavaScript program
► using an external Java script file
► the object model in JavaScript
► the document.write() method
► Java Script variables and operators.
SNAP RECAP: To assess you pre- knowledge
► list the elements of HTML you have learned so far .
▪ JavaScript is one of the most popular scripting language for the internet it
works in all major browsers such as :
► Internet Explorer
► Firefox
► Chrome
► Opera and Safari
The Main Objective in this Chapter:
▪ We will learn how to add functionality to your webpages ,using JavaScript.
▪ Is JavaScript Free?
<script language="JavaScript">
…… The section is called
[JavaScript statements] scripting block.
……
</script>
</html>
JavaScript may be placed in either the in
<head> or <body>
<script language="JavaScript">
………
</script> different sections
<script language="JavaScript">
………
</script>
</html>
• It's recommended to keep all the JavaScript code together in one single
section of the document this is done for better readability and
understanding.
JavaScript Where To:
1) JavaScript in <head>
2) JavaScript in <body>
3) Or in both
4) External JavaScript
1) JavaScript in <head>
JavaScript in <head>
In this example, a JavaScript function is placed in the <head> section of an HTML page.
For example:
<html>
<head>
</head>
<body>
………
</body>
</html>
2) JavaScript in <Body>
JavaScript in <body>
In this example, a JavaScript function is placed in the <body> section of an HTML page.
For example:
<html>
<head>
…………
</head>
<body>
</body>
</html>
3) JavaScript in both
In this example, a JavaScript function is placed in the <head> section and in <body>
section of an HTML page.
For example:
<html>
<head>
<script language="JavaScript">
…………
</script> In both sections
</head>
<body>
<script language="JavaScript">
…………
</script>
</body>
</html>
4)Using an external JavaScript file
• JavaScript can also be placed in external files:
• To use the external Script ,point to the .js file in the ‘src’ attribute of the script tag
in a HTML document.
for example:
</html>
<script language="JavaScript" src="Myscript.js">
X
<script language="JavaScript">
External scripts cannot
contain <script> tags
function myFunction()
{
document.getElementById("demo").innerHTML = "Paragraph changed.";
}
X
</script>
Searching information (5mins)
Task:
Placing scripts in external files has some
advantages(use the internet to search
for some) and then discussing them with
your colleague.
External file with name myScript.js
External scripts
cannot
contain <script> tags
Calling an External
scripts (Myscript.js) in
HTML Document
HTML Document
3) Using an external JavaScript file
Placing scripts in external files has some advantages:
• It separates HTML and code
• It makes HTML and JavaScript easier to read and maintain
• Cached JavaScript files can speed up page loads
• If you want to run the same JavaScript on several pages without having to write the same script on
every page
An external script can be referenced in 3 different ways:
• With a file path (like /js/) : <script src="/js/myScript.js"></script>
You should follow the rules given below when writing a program using
JavaScript:
• The scripting language has to be written within <script>tag and
</script>tag in a HTML document.
An Example:
• The while keyword, for example, must be typed “while”, not
“ While” or “ WHILE”.......
let a, b, c;
a = 5;
b = 6
c = a + b;
document.write(5 + 6);
• This means that you think about your JavaScript program in terms of the
objects you want to work with .
• You can access information about the objects with the help of methods .
• Methods are functions connected with objects.
• In other words ,the actions you can perform on or with objects are called
Method
• We will learn to use simple object and method to write programs in
JavaScript.
Object.Method()
The Object Model in JavaScript :
For any Object that has a Method:
1. Type the name of the object
2. Followed by a DOT
3. Then followed by the name of the method
4. And the set of parenthesis Object.Method()
Example:
Car . Travel ( )
❖ document.write()
❖ document.writeln()
❖ document.getElementById()
❖ window.alert()
❖ window.confirm()
❖ window.prompt()
document .write() Method:
The document.write() method is used for displaying the text directly on the
" or '
document.write("welcome to JavaScript");
Object called 'document 'which The method write() of this String or Text that you
refers to the current document of object is to display a string or text want to display on the
the browser window on the screen . browser window.
document .write() Method:
The document.write() method is used for displaying the text directly on the
browser window.
<html>
<head>
<title>Welcome</title>
</head>
<body>
<script language="JavaScript">
document.write("Welcome To JavaScript");
</script>
</body>
</html>
Welcome To JavaScript
A simple JavaScript program:
• To display a simple text on the browser windows as shown below the following
script has to be written. Write some text directly to the HTML output:
<html>
<head>
<title>Welcome</title>
</head>
<body>
<script language="JavaScript">
document.write("Hello World!");
document.write("Welcome To JavaScript");
The two statements will
document.write('Hello World!'); appear on the same line
document.write('Welcome To JavaScript');
</script>
</body>
</html>
A simple JavaScript program:
• To display a simple text on the browser windows as shown below the following
script has to be written. Write some text directly to the HTML output using
<br> tag:
<html>
<head><title>Welcome</title></head>
<body> The two statements will appear on the
<script language="JavaScript"> different line
document.write("Hello World!");
document.write("<br>");
document.write("Welcome To JavaScript");
document.write("<br>");
document.write("Hello <br> World!");
</script>
</body>
</html>
document .write() Method:
The document.write() method is used for displaying HTML elements
directly on the browser window.
" or '
Computer Science
document .write() Method:
The document.write() method is used for displaying date object
directly on the browser window.(without Quotation)
<script language="JavaScript">
document.write(Date());
</script>
</body>
</html>
document .writeln() Method:
The document.writeln() method is identical to the write() method, with the
addition of writing a newline after each statement.
document.writeln("welcome to JavaScript");
Object called 'document
The method write() of this String or Text that
'which refers to the current
object is to display a string or text you want to display on
document of the browser
on the screen . the browser window.
window
3. Display the heading ‘I'm reading JavaScript’ the in a big font size (6)
Center aligned and underlined.
The Answer Key
<html>
<head><title>Activity</title></head>
<body>
<script language="JavaScript">
document.write("<h2><i>Hello</i></h2>");
<script language="JavaScript">
document.write('<font color="blue"><center>Sara Smith</center>
</font>');
document.write("<br>") </script>
<script language="JavaScript">
document.write('<font size="6"><center><u>I am learning
JavaScript</u></center></font>');
</script>
</body>
</html>
.
JavaScript can change HTML content using button
The button is a clickable button .The onclick event occurs when the
user clicks on an HTML element .
<button type= " " onclick= " ">value</button>
When clicking
on button
document.getElementById(" ").innerHTML:
• One of many JavaScript HTML methods is getElementById()is used to
Find an element by element id and Get the HTML content of an element
with id= " "
<button type="button"
onclick='document.getElementById("demo").innerHTML = "Hello
JavaScript!"'> Click Me! </button>
</body>
</html>
The HTML code using Comparison operators :
<html>
<head><title>Welcome</title></head>
<body>
<p id="demo"></p>
<h3>Comparison</h3>
<script language="JavaScript">
let x=5;
let y=5;
document.getElementById("demo").innerHTML=(x==y);
</script>
</body>
</html>
Adding comments to JavaScript Code:
• before you learn more about methods in JavaScript you learn how to add
comment to the JavaScript code
• JavaScript comments can be used to explain JavaScript code, and to make
it more readable.
?
document.write(Date());
</script> s wi th
b e r t be gin
•Multi-line Comments: e m em m m en ta g
R c o – >
<script language="JavaScript"> H T ML e n t –
An co m m
/* < ! – –
The code below will change
in my web page:
*/
document.write(Date());
</script>
Activity:
Create a web page including the following:
1. Add a single line comment to JavaScript Code after the last line with "
"Adding Date Object "
<script language="JavaScript">
//Adding Date Object ①
document.write(Date()); ②
</script>
<p id="demo"> Goodbye HTML</p>
<script language="JavaScript">
let x=5;
let y=5;
document.getElementById("demo").innerHTML=(x==y);
</script>
</body>
</html>
True