Unit 5
JavaScript
‘* JavaScript is the world's most popular programming language.
* JavaScript is the programming language of the Web.
* JavaScript is easy to lean.
Why Study JavaScript?
Javascript is one of the 3 languages all web developers must learn:
1. HTML to define the content of web pages
2. CSS to specify the layout of web pages
3, JavaScript to program the behavior of web pages
What you can do with javascript?
JavaScript Can Change HTML Content
‘One of many JavaScript HTML methods is getElementByld().
The example below "finds" an HTML element (with id="demo"), and changes the element content
(innerHTML) to "Hello JavaScript"
document.getlementByld("'demo").innerHTML
lello JavaScript";
JavaScript Can Change HTML Attribute Values
JavaScript Can Change HTML Styles (CSS)
Changing the style of an HTML element, is a variant of changing an HTML attribute:
document.getElementByld("demo").style.fontSize Spx";
JavaScript Can Hide HTML Elements
Hiding HTML elements can be done by changing the display style:
document.getElementByld("demo").style.display = "none";
JavaScript Can Show HTML Elements
Showing hidden HTML elements can also be done by changing the display style:
document getElementByld("demo").style.display = "block";
The
code is inserted between tags.
Old JavaScript examples may use a type attribute:
Demo JavaScript in Head
’aragraph changed.";
A Paragraph
JavaScript in
In this example, a JavaScript function is placed in the section of an HTML page.
The function is invoked (called) when a button is clicked:
Demo JavaScript in Body
A Paragraph
'myFunction()">Try its/button>
Notes - CSCO 2302 Introduction to Web Technology 2022-23 (Ms. Londhe K.W.) 2External JavaScript
Scripts can also be placed in external files:
function myFunction() {
document. getélementByld("demo").innerHTML = "Paragraph changed.";
}
External scripts are practical when the same code is used in many different web pages.
JavaScript files have the file extension .js.
To use an external script, put the name of the script file in the src (source) attribute of
a
You can place an external script reference in or as you like.
The script will behave as if it was located exactly where the
External References
An external script can be referenced in 3 different ways:
With a full URL (a full web address)
With a file path (like /js/)
jout any path
This example uses a full URL to link to myScript.js:
This example uses a file path to link to myScript js:
This example uses no path to link to myScript.js:
JavaScript Display Possil S
JavaScript can “display” data in different ways:
Writing into an HTML element, using innerHTML.
Notes - CSCO 2302 - Introduction to Web Technology 2022-23 (Ms, Londhe K.W.)‘* Writing into the HTML output using document.write().
‘* Writing into an alert box, using window.alert()
* Writing into the browser console, using console.log().
Using innerHTML
To access an HTML element, JavaScript can use the document.getElementByld(id) method
The id attribute defines the HTML element. The innerHTML property defines the HTML conter
DOCTYPE htmi>
My First Web Page
My First Paragraph
Using document.write|
For testing purposes, it is convenient to use document.writel):
My First Web Page
My first paragraph.
Using window.alert()
You can use an alert box to display data:
My First Web Page
My first paragraph.
Notes - CSCO 2302 Introduction to Web Technology 2022-23 (Ms. Londhe K.W.)You can skip the window keyword.
In JavaScript, the window object is the global scope object. This means that variables, properties, and
methods by default belong to the window object. This also means that specifying the window keyword is
optional:
IDOCTYPE htmi>
My First Web Page
My first paragraph.
Using console.log|
For debugging purposes, you can call the console.log() method in the browser to display data.
Script Print
JavaScript does not have any print object or print methods.
You cannot access output devices from JavaScript.
The only exception is that you can call the window.print() method in the browser to print the content of
the current window.