Javascriptsession1 180528082723
Javascriptsession1 180528082723
INTRO TO JAVASCRIPT
SESSION 12
Explain scripting
Explain the JavaScript language
Explain the client-side and server-side JavaScript
List the variables and data types in JavaScript
Describe the JavaScript methods to display
information
Explain escape sequences and built in functions in
JavaScript
Explain events and event handling
Explain jQuery
Describe how to use the jQuery Mobile
Presented by Muhammad Ehtisham Siddiqui (BSCS)
CASE
3
CASE:
Consider an organization that provides a Web site that allows its customers to view
their products. The company has received frequent customer feedbacks to provide
the shopping facility online. Therefore, the company has decided to add the
shopping facility in their Web site by creating dynamic Web pages. These Web
pages will allow the user to shop for the products online. Here, the main task of the
developer is to validate the customer’s inputs while they shop online. For example,
details such as credit card number, email, and phone number entered by the
customer must be in a proper format. Further, the developer also needs to retrieve
the chosen products and their quantity to calculate the total cost.
SOLUTION
The developer can handle all these critical tasks by using a scripting language. A
scripting language refers to a set of instructions that provides some functionality
when the user interacts with a Web page.
Scripting languages are often embedded in the HTML pages to change the behavior
of the Web pages according to the user’s requirements.
JavaScript is a scripting language that allows you to build dynamic Web pages
by ensuring maximum user interactivity.
In real life, an object is a visible entity such as a car or a table. Every object
has some characteristics and is capable of performing certain actions.
The identity of the object distinguishes it from the other objects of the same
type. The state of the object refers to its characteristics, whereas the behavior
of the object consists of its possible actions.
The object stores its identity and state in fields (also called variables) and
exposes
Presented its behavior
by Muhammad Ehtishamthrough
Siddiqui functions (actions).
(BSCS)
Intro to JavaScript
7
A server-side JavaScript (SSJS) is executed by the Web server when an HTML page
is requested by a user.
The output of a server-side JavaScript is sent to the user and is displayed by the
browser.
.In this case, a user might not be aware that a script was executed on the server to
produce the desirable output.
Suppose, if the browser does not support the <video> element then the content
between the start tag and end tag is displayed on the browser.
A server-side JavaScript can interact with the database, fetch the required
information specific to the user, and display it to the user. This means that server-
side scripting fulfills the goal of providing dynamic content in Web pages. Unlike
client-side JavaScript, HTML pages using server-side JavaScript are compiled into
bytecode files on the server. Compilation is a process of converting the code into
machine-independent code. This machine-independent code is known as the
bytecode, which is an executable file. The Web server runs this executable to
Presented by Muhammad
generate Ehtisham
the desired Siddiqui
output.
(BSCS)
Server-Side JavaScript
10
The <script> tag defines a script for an HTML page to make them interactive
The browser that supports scripts interprets and executes the script specified under
the <script> tag when the page loads in the browser.
You can directly insert a JavaScript code under the <script> tag. You can define
multiple <script> tags either in the <head> or in the <body> elements of an HTML
page.
In HTML5, the type attribute specifying the scripting language is no longer required
as it is optional.
There are two main purposes of the <script> tag, which are as follows:
Identifies a given segment of script in the HTML page
Loads an external script file
<!DOCTYPE html>
<html>
<head>
<script>
document.write(“Welcome to the Digital World”);
</script>
</head>
<body>
<h1>This is First JavaScript Code</h1>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h2>My First JavaScript</h2>
<button type="button"
onclick="document.getElementById('demo').innerHTML = Date()">
Click me to display Date and Time.</button>
<p id="demo"></p>
</body>
</html>
A variable refers to a symbolic name that holds a value, which keeps changing.
A real life example for variables includes the variables used in algebraic
expressions that store values.
<!DOCTYPE html>
<html><head>
<script>
function myFunction() {
document.getElementById("demo").innerHTML = "Paragraph
changed.";
}
</script>
</head>
<body>
<h1>A Web Page</h1>
<p id="demo">A Paragraph</p>
<button type="button" onclick="myFunction()">Try it</button>
</body>
</html>
<!DOCTYPE html>
<html><head>
<script>
function ChangeCss(){
document.getElementById("JCss").style.backgroundColor="Red";
}
</script></head>
<body>
<h1>A Web Page</h1>
<p id="demo">This is Iran</p>
<p id="JCss">This is Pakistan</p>
<button id="btn" onClick="myFunction()">Check Function</button>
<button id="btn" onClick="ChangeCss()">Change Css</button>
</body>
</html>
<!DOCTYPE html>
<html>
<head><title>JS Alert</title>
<script>
function myFunction() {
alert("You have created alert box");
}
</script></head>
<body>
<h2>JavaScript Alert</h2>
<button onclick="myFunction()">Try it</button>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<script>
function myFunction() {
var person = prompt("Please enter your name", "Harry Potter");
if (person != null) {
document.getElementById("demo").innerHTML =
"Hello " + person + "! How are you today?"; }}
</script>
</head>
<body>
<p>Click the button to demonstrate the prompt box.</p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
</body>
</html>
The event
The event
The event bubbling
The user handler is
object is occurs as the
performs an The event is invoked that
updated to event bubbles
action to raise fired. performs the
determine the through the
an event. specified
event state. elements of
actions.
the hierarchy.
Events Description
onmousedo Occurs when the mouse button is pressed
wn
onmouseup Occurs when the mouse button is released
onclick Occurs when the mouse button is pressed and release
ondblclick Occurs when the mouse button is double-clicked
onmousemo Occurs when the mouse pointer is moved from one location to
ve other
onmouseove Occurs when the mouse pointer is moved over the element
r
onmouseout Occurs when the mouse pointer is moved out of the element
Events Description
onmousedo Occurs when the mouse button is pressed
wn
onmouseup Occurs when the mouse button is released
onclick Occurs when the mouse button is pressed and release
ondblclick Occurs when the mouse button is double-clicked
onmousemo Occurs when the mouse pointer is moved from one location to
ve other
onmouseove Occurs when the mouse pointer is moved over the element
r
onmouseout Occurs when the mouse pointer is moved out of the element
myScript.js
<!DOCTYPE html>
<html>
function myFunction() {
<body>
<h2>External JavaScript</h2> document.getElement
<p id="demo">A Paragraph.</p> ById("demo").innerHTM
<button type="button" L = "Paragraph
onclick="myFunction()">Try changed.";
it</button> }
<p>(myFunction is stored in an
external file called "myScript.js")</p>
<script src="myScript.js"></script>
</body></html>
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 <script> tag.
To add several script files to one page - use several script tags:
<script src="myScript1.js"></script>
<script src="myScript2.js"></script>