[Go to site: main page, start]

0% found this document useful (0 votes)
4 views28 pages

Lecture 17 - JavaScript

This document discusses scripting, its applications, and specifically focuses on web scripting, differentiating between client-side and server-side scripts. It introduces JavaScript, its history, and usage in web development, including examples of basic scripts and variable handling. The document also covers the functionality of JavaScript in creating interactive web experiences and handling user inputs.

Uploaded by

Tayyab tariq
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views28 pages

Lecture 17 - JavaScript

This document discusses scripting, its applications, and specifically focuses on web scripting, differentiating between client-side and server-side scripts. It introduces JavaScript, its history, and usage in web development, including examples of basic scripts and variable handling. The document also covers the functionality of JavaScript in creating interactive web experiences and handling user inputs.

Uploaded by

Tayyab tariq
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

109203 – Applications of ICT

Lecture 17

Engr. Mahnoor Iftikhar

1
Scripting?
• Scripting is a way to instruct a computer software, in how to control a
computer hardware or a machine.
• Computer software are made through programming, and software
can be controlled using scripting.
• Just like programming languages, scripting languages do have a valid
syntax and they need a compiler.

2
Where are scripts used?
• Scripting maybe used in:
Controlling Robots (eg: LEGO kits)
Controlling 3D design software(Maya,3d Studio)
Controlling operating systems (Windows Batch scripting)
Controlling websites (Javascript, VBSript)

3
Web Scripting
• Web scripting refers to writing code or scripts that run on web pages
to create dynamic and interactive user experiences.
• These scripts are typically used to control the behavior of web
elements, handle user inputs, and interact with web servers or
databases.
• Web scripting languages add to the dynamics of websites.
• They are used to add interactivity to web applications.
• You come across web scripts daily while browsing the internet, for-
example the chatrooms, Discussion forums, Email services, etc are all
web scripts

4
Types of web-scripts
• There are two kinds of web-scripts:
i. Client side scripts
ii. Server side scripts

5
Client side scripts
• Client side scripts run on the user/client’s machine, they may or may
not interact with a server, and are ideal for quick response.
• This type of script runs on the user's browser (client-side) rather than
the web server. It is used to create interactive user interfaces and
handle user input.
• Used for pre-submission form checking like checking if the user has
entered a valid email address or phone number etc.
• Example: Javascript, VBScript, etc

6
Client side scripts
Use Cases:
• Form validation
• Animations
• Dynamic content updates (without reloading the page)
• User interface behavior enhancements (dropdown menus, modal
windows, etc.)

7
Server side scripts
• Server-side scripts run on the web server and process requests from the client
(browser). They are responsible for generating dynamic web content that is sent
to the client.
• Server side scripts run on-line on the server.
• All data must be uploaded to the server in-order for server side scripts to manage
it.
• The Server resources are utilized in the process and the execution is independent
of the client’s machine.
• Server side scripts are useful for storing user accounts, sessions, and other secure
data.
• Examples: PhP, ASP, [Link], etc.
8
Introduction to Javascript
• Developed by Brendan Eich of Netscape under the name ‘Mocha’, later renamed
to ‘Livescript’.
• With the advent of ‘Java’, netscape renamed Livescript to Javascript as a
marketing strategy, creating an artificial similarity between Java and Javascript.
• Javascript is just a spin-off of java, they’ve got nothing in common other than the
fact that both try to follow the C++ syntax.

9
Introduction to Javascript (2)
• Javascript was originally a client-side scripting language.
• It suffered a hibernation period when webmasters started using server side
scripting.
• With the advent of AJAX, javascript returned to the spotlight, as AJAX is a server-
side port of javascript.
• Javascript’s compiler is embedded in almost all modern web-browsers so we
don’t need any external software to run javascript applications.

10
Introduction to javascript (3)
• We may use javascript in our websites by including the code in the
<script> tag.
• The scripting language is specified using the “language” attribute.
• Example:
<script language=“javascript”>
….all the scripting….
</script>

11
A basic “Hello World” application

• Write down the following code in the body of an


HTML document and save it as .html file, check the
output by running the html document in a web-
browser(ignore the text after ‘//’ its for reference).

12
“Hello world” Output

13
A javascript input script

Let us consider another script:

14
A javascript input script : Output

15
A javascript input script : Explanation

• Notice that this script takes some input from the user.
• ‘Window’ is a JavaScript object.
• Objects have member functions or methods, prompt is a method which takes
some inputs called arguments.
• The default value in the table is specified by the second argument to the prompt
function(150 in this case).

16
The windows alert function

• Used to display a message to the user, about an


event.
• Consists of a custom message and an OK button.
• Example:

17
window alert: Output

18
Javascript Variables
• A variable is a piece of computer memory for temporarily holding
some data.
• The data can be a name, a telephone number, a temperature, etc.
• In almost all programming languages, variables must reserve a
memory space before data can be written to them, this process is
called declaring a variable.

19
Javascript Variables
• In javascript, we declare a variable using the ‘var’ keyword.
• We assign a value to it using the assignment operator ‘=‘.
• The variable must have a name (also called ‘identifier’).
• Example: var variable_name=“Some value to the variable”;
var age = 20;

20
Javascript Variables(3)
• We can assign value to the variable in the code, or we can take the
value from the user.
• As already discussed, we use the prompt function to take a value
from the user, and store it in a variable for later reuse.

21
Javascript Variables: Example
• Consider the following code:
<script language=“javascript”>
var var1=[Link](“Enter your name:”);
[Link](var1);
</script>

22
Variable Example: Output

23
Variable Example 2
• Let us make a simple adder script, which takes two numbers from the
user and displays their sum.
• All the data stored in a variable is a string by default, we need to
convert it to our required data-type before we can do any operations.
• Here we need it to be an integer number so we use the ‘parseInt’
function which converts a string to an integer.

24
Number Adder: Code

25
Number Adder: output

26
HTML in Javascript

• We can use the Write method to display HTML in


a document, for-example:

27
HTML in Javascript Output

28

You might also like