Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 27
Introduction to Javascript
Lecture number: 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. 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) Web Scripting
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 Types of web-scripts
There are two kinds of web-scripts:
i. Client side scripts ii. Server side scripts 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. 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 Server side scripts
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 (Personal home page tool, open source general-purpose server-side scripting language originally designed for Web development to produce dynamic Web pages.), ASP(active server pages, 1 server side script engine for st
dynamically generated webpages), ASP.net, etc.
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. 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 (asynchronous javascript ,a group of interrelated web development techniques used on the client-side to create asynchronous web applications.), 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. 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> 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). “Hello world” Output A javascript input script
Let us consider another script:
A javascript input script : Output 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). 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: window alert: Output 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. 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”; 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. Javascript Variables: Example
Consider the following code:
<script language=“javascript”> var var1=window.prompt(“Enter your name:”); document.write(var1); </script> Variable Example: Output 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. Number Adder: Code Number Adder: output HTML in Javascript
We can use the Write method to display
HTML in a document, for-example: HTML in Javascript Output
JavaScript Fundamentals: JavaScript Syntax, What JavaScript is Use for in Website Development, JavaScript Variable, Strings, Popup Boxes, JavaScript Objects, Function, and Event Handlers: JavaScript Syntax, What JavaScript is Use for in Website Development, JavaScript Variable, Strings, Popup Boxes, JavaScript Objects, Function, and Event Handlers
JavaScript Fundamentals: JavaScript Syntax, What JavaScript is Use for in Website Development, JavaScript Variable, Strings, Popup Boxes, JavaScript Objects, Function, and Event Handlers
JavaScript Fundamentals: JavaScript Syntax, What JavaScript is Use for in Website Development, JavaScript Variable, Strings, Popup Boxes, JavaScript Objects, Function, and Event Handlers: JavaScript Syntax, What JavaScript is Use for in Website Development, JavaScript Variable, Strings, Popup Boxes, JavaScript Objects, Function, and Event Handlers
JavaScript Fundamentals: JavaScript Syntax, What JavaScript is Use for in Website Development, JavaScript Variable, Strings, Popup Boxes, JavaScript Objects, Function, and Event Handlers