XML Is A Mark-Up Language Much Like HTML.: XML Is Designed To Store and Transport Data
XML Is A Mark-Up Language Much Like HTML.: XML Is Designed To Store and Transport Data
*HTML used to display data; XML is used to Store & transfer data.
<body>
<p></p>
<h1></h1>
</body>
</html>
<child2>
<sub></sub>
<sub></sub>
</child2>
</root>
*Here , starting tag and ending tag should be same and case sensitive.
*Similar to HTML , we can use attributes in XML @ starting tag.
Ex:college.xml
<college>
<student>
<name>Ganesh Peketi</name>
<rollno>14191A0155</rollno>
</student>
<faculty>
<name>Siva Kumar</name>
<branch>CSE</branch>
</faculty>
<HOD college=”GOVT”/>
<principal> Dr.Murali</principal>
</college>
Client side script: These scripts are getting executed within the web Browser (client).
Ex: JavaScript, VB script
Server side script: A script which executes in server machine with support of the web
server software’s like IIS(Internet information services), Tomcat, JBOSS, etc.
Ex: php, jsp, asp.net, nodeJS, reactJS
JavaScript Introduction
Using HTML we can only design a web page but it’s not supported to perform
logical operation such as calculations, decision making and repetitive tasks,
dynamically displaying output, reading inputs from the user, and updating
content on webpage at client side. Hence to perform these entire tasks at client
side we need to use JavaScript.
Where it is used?
There are so many web applications running on the web that are using JavaScript
like Google, Facebook,twitter, amazon, YouTube etc.
It is used to create interactive websites. It is mainly used for:
1. Client-side verifications and validation
2. Dynamic drop-down menus
3. Displaying date and time
4. Build small but complete client side programs
5. Build forms that respond to user input without accessing a server.
6. Displaying popup windows and dialog boxes (like alert dialog box, confirm
dialog box and prompt dialog box)
7. Manipulate HTML "layers" including hiding, moving, and allowing the user
to drag them around a browser window.
etc...
Limitations of JavaScript
JavaScript have some limitations which are given below;
JavaScript Versions
Version Officeal Name Release Date
1 ECMAScript 1 June-1997
2 ECMAScript 2 June-1998
3 ECMAScript 3 Dec-1998
5 ECMAScript 5 Dec-2009
5.1 ECMAScript 5.1 June-2011
6 ECMAScript June-2015
7 ECMAScript June-2016
8 ECMAScript June-2017
>internal scripting
> scripting in head sec
head is first executed part of html, hence javascript is also executes first.
<head>
<script type="text/javascript>
JS code
</script>
</head>
> scripting in body sec
body level script is executed after head section
<body>
<script type="text/javascript>
JS code
</script>
</body>
Comments in JavaScript
Comment is nothing but it is a statement which is not display on browser
window. It is useful to understand which code is written for what purpose.
Comments are useful in every programming language to deliver message. It is
used to add information about the code, warnings or suggestions so that the end
user or other developer can easily interpret the code.
Types of Comments:
There are two types of comments are in JavaScript
1. Single-line Comment ex: //comment
2. Multi-line Comment e: /* comments */
Single-line Comment
It is represented by double forward slashes //. It can be used before any
statement.
Example:
<script>
// It is single line comment
document.write("Hello Javascript");
</script>
Multi-line Comment
It can be used to add single as well as multi line comments.
It is represented by forward slash / with asterisk * then asterisk with forward
slash.
Example:
<script>
/* It is multi line comment.
It will not be displayed */
document.write("Javascript multiline comment");
</script>
Example:
<html>
<head>
<script type='text/javascript'>
document.write("<h1>hello world!</h1><p> have a nice day ! </p>");
</script>
</head>
</html>
writeln() method: The writeln() method is similar to the write method, with the
addition of writing a newline character after each statement.
Syn:document.writeln(exp1,exp2,exp3 ...)
Example:
<!DOCTYPE html>
<html>
<body>
<pre>
<script type='text/javascript'>
document.writeln("Welcome to JS");
document.writeln("Welcome to JS");
</script>
</pre>
</head>
</html>
Note: You have to place writeln() in pre tag to see difference between write()
and writeln().
Writeln() actually produces the output in new line (\n) but browser will not
detect the \n as linebreak, hence to show it correctly and keep format as it is
we will use pre tag.
Example:
<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'>
document.write("<h1 style='color:blue; font-size:30px; font-family:tahoma'>
Welcome To JS</h1>");
document.write("<font color='green' size='16px' face='Arial'> Welcome To
JS</font>");
</script>
</head>
<body>
</body>
</html>
Note:
the above type of code is known as DHTML
In JavaScript a string should be in single or double quotes.
Double quotes inside using single quotes are valid, single quotes inside
using double quotes valid.
Example
<!DOCTYPE html>
<html>
<body>
<script>
document.write("JavaScript is client side script");
document.write("<br>");
document.write("JavScript is 'ECMA' Implementation<br>");
document.write('JavScript released by NetScape<br>');
document.write('NetScape release "Mocha"<br>');
//document.write('NetScape release 'Mocha'<br>'); Error
//document.write("NetScape release "Mocha"<br>"); Error
</script>
</body>
</html>
Example:
document.write();
| |
page method
ex:
<head>
<script type='text/javascript'>
window.document.write("livescript is javascript");
document.write("<br>");
document.write('livescript is javascript');
</script>
</head>
JavaScript semicolon(;):
In javascript every statements ends with semicolon(;). It is an optional notation.
ex:
<head>
<script type='text/javascript'>
document.write=("livescript is javascript");
</script>
</head>
ex:
<head>
<script type='text/javascript'>
document.write("javascript");
document.write('livescript');
document.write('livescript is javascript');
</script>
</head>
Note:
1) In the above script semicolon(;) is mandatory.
2) It is a good programming practice to use the semicolon.
ex:
<head>
<script type='text/javascript'>
document.write("welcome to head section");
</script>
</head>
<body>
<script language="javascript">
document.write("welcome to the body section");
</script>
</body>
External javascript:
Javascript can also be placed in a external files,these files contains javascript
code. This code we can apply on different webpages. External javascript files
extensions is .js
Note:
1) External script cann't contain the <script></script> tags.
2) To use an external script,point to the .js file in the "src" attribute of the
<script> tag.
Alert box: An alert box is often used if you want to make sure information comes
through the user. When an alert box pops up, the user will have to click "ok" to
proceed.
Syn: alert("message");
ex:
<body>
<script type='text/javascript'>
alert("invalid entry");
</script>
</body>
confirm box:
It is often used, If you want the user to verify and accept something. When a
confirm box pops up, the user will have to click either "ok" or "cancel" to
proceed. If the user clicks "ok" the box returns true. If the user clicks "cancel" the
box returns "false".
Syntax: confirm("message");
ex:
<head>
<script type='text/javascript'>
confirm("click ok or cancel");
</script>
</head>
ex:
<head>
<script type='text/javascript'>
var x=confirm("click ok or cancel");
alert("user selected option is:"+x);
</script>
</head>
ex:
<head>
<script type='text/javascript'>
var x=confirm("click ok or cancle");
alert("user selected option is:"+x);
if(x==true) {
alert("user clicked on OK button");
}
else{
alert("user clicked on cancel button");
}
</script>
</head>
Prompt Box: It is used to, if you want the user to input a value before entering a
page. When a prompt box pops up the user will have to click either "ok" or
"cancel" to proceed after entering an input value. If the user clicks "ok" the box
returns the value/empty. If the user clicks "cancel" the box returns "null".
Syntax: prompt("sometext", defaultvalue);
ex:
<head>
<script type='text/javascript'>
prompt("Enter Any Number:");
</script>
</head>
ex:
<head>
<script type='text/javascript'>
varMyVal=prompt("Enter Any Number:");
alert("User Entered value is:"+MyVal);
</script>
</head>
Inline JavaScript
When java script was written within the html element using attributes related to
events of the element then it is called as inline java script.
Internal JavaScript
When java script was written within the section using element then it is called as
internal java script.
Writing java script in a separate file with extension .js is called as external java
script. For adding the reference of an external java script file to your html page,
use tag with src attribute as follows
Example
<script type="text/javascript" src="filename.js"/>
Create a file with name functions.js and write the following java script
functions in it.
a.js
functionmsg()
{
alert("Welcome to JavaScript");
}
Create a html page and use the file functions.js as follows
index.html
<html>
<head>
<script type="text/javascript" src="a.js"></script>
</head>
<body>
<form>
<input type="button" value="click" onclick="msg()"/>
</form>
</body>
</html>
Variable Declaration
variable is a reference name of a memory block.
variable are created or stored in RAM.
variable are used to store/to hold a value for reuse purpose.
Java script did not provide any data types for declaring variables and a variable in
java script can store any type of value. Hence java script is loosely typed
language.
Variable declaration
Example
var x = 10; // Valid
var _value="porter"; // Valid
var 123=20; // Invalid
var #a=220; // Invalid
var user name="siva"; // Invalid
var case=10.56;
<script>
var a=10;
var b=20;
var c=a+b;
document.write(c);
</script>
Output
30
Local Variable
Global Variable
Local Variable
Ex: JS8.html
For example:
<script>
function abc()
{
var x=10; //local variable
}
</script>
or
Example
<script>
If(10<13)
{
var y=20;//javascript local variable
}
</script>
Global Variable
var is declared with in script tag but outside function & block those are global
variables.
these global variable is accessible from anywhere in program.
declared with window object is known as global variable.
Ex: JS9.html
For example:
<script>
var value=10;//global variable
function a()
{
alert(value);
}
function b()
{
alert(value);
}
</script>
For example:
function m()
{
window.value=200; //declaring global variable by window object
}
function n()
{
alert(window.value); //accessing global variable from other function
}
Scripts are able to support implicit declaration but languages are only explicit
declaration.
Note: Explicit declaration is always recommended as a good programming
practice.
Javascript datatypes:
In javascript data types are classified into the following two types.
1. primitive datatypes
2. non-primitive datatypes
typeof
typeof is predefine function, and it's used to identify datatype of a variable.
Syn: typeof var-name
Ex: JS10.html
Dynamic data types: Javascript has dynamic types. This means that the same
variable can be used as different types.
ex:
var x; //now x is undefined
var x=5; // now x is a number
var x="ram"; // now x is a String
Ex: JS11.html
<!-- example on variable declaration -->
<!DOCTYPE html>
<html lang="en">
<head>
<title>JS Ex13</title>
</head>
<body>
<h1> Demo on difference between var and let </h1>
<script>
var a=10; //define
document.write(a +"<br>");
var a=20.56; //re-defination
document.write(a +"<br>");
var a="apple"; //re-defination
document.write(a +"<br>");
Non-primitive data types: When a variable is declared with the keyword new,
the variable is an object.
new is used for dynamic memory allocations (for creating objects).
these datatypes are also called as reference datatype.
Ex:
var st=new String();
var x=new Number();
let y=new Boolean();
here LHS are reference variables, and RHS are objects.
reference variables are storing address of dynamic memory (object)
JavaScript operators
operator is a symbol (special char) and it is used to perform certain
operation(task).
every operator is a symbol, but every symbol is not operator.
every operator requires some values, those are called as operands.
Ex:
ternary operator: this operator are used to decision making operation. operator
is ?:, this operator also called as conditional operator.
(condition) ? statement1 : statement2
concat operator: this operator are used to concat multiple strings then formed
into a single string. one operand should be string to perform concatenation.
resultant value comes in string format.
operator is +.
Ex: "rama"+"rao ==> "ramarao"
"mangos"+123 ==> "mangos123"
ParseInt():
Ex:JS16,17
Control Statement
Control statements are used to control (change) execution flow of program
based input data.
Types:
> Conditional statements (Decision making statements’)
> loops (iterations)
> Un-conditional statements (jump)
Conditional Statements:
If Statement
if statement is used to perform decision-making operations. Means if condition is
true it executes some statements, if condition is false it executes some other
statements.
If statement
if is most basic statement of Decision making statements. It tells to program to
execute a certain part of code only if particular condition or test case is true.
Example
<script>
var a=10;
if(a>5)
{
document.write("value of a is greater than 5");
}
</script>
if-else statement
In general it can be used to execute one block of statement among two blocks.
Result:
a is even number
Syntax
if(expression1)
{
//content to be evaluated if expression1 is true
}
else if(expression2)
{
//content to be evaluated if expression2 is true
}
else
{
//content to be evaluated if no expression is true
}
Example of if..else if statement
<script>
var a=40;
if(a==20)
{
document.write("a is equal to 20");
}
else if(a==5)
{
document.write("a is equal to 5");
}
else if(a==30)
{
document.write("a is equal to 30");
}
else
{
document.write("a is not equal to 20, 5 or 30");
}
</script>
Note:
> if support 6types relational operators (condition)
> comp styles:
> var to var a>b
> var to val n<0
> val to val 1>2
> val to var 1<a
> if supports all 4
> if supports all datatypes
switch statement
> Switch is selection statement, but it is not decision making.
> Switch is not allows to use relational operators, but its performs equality
operation.
> Its supports all types comparison style and all datatypes.
> Its better performance.
Syn:
switch(var)
{
case value: statements...
break;
case value: statements...
break;
case ...
default: statements...
}
Looping Statement
Set of instructions given to the compiler to execute set of statements until
condition becomes false is called loops. The basic purpose of loop is code
repetition.
The way of the repetition will be forms a circle that's why repetition statements
are called loops. Some loops are available In JavaScript which are given below.
while loop (top testing/entry level)
for loop
do-while (bottom testing/exit level)
while loop
When we are working with “while loop” always pre-checking process will be
occurred. Pre-checking process means before evolution of statement block
condition part will be executed. “While loop” will repeat in clock wise direction.
Example of while loop
<script>
var i=10;
while (i<=13)
{
document.write(i + "<br/>");
i++;
}
</script>
do-while loop
In implementation when we need to repeat the statement block at least 1 then
go for do-while. In do-while loop post checking of the statement block condition
part will be executed.
<noscript> tag: It is used to provide an alternate contains for users when script is
disabled or not supporting, It is a paired tag. It is always declared within the body
section.
syntax: <noscript>------</noscript>
ex:
<head>
<script type='text/javascript'>
alert("welcome to js");
</script>
</head>
<body>
<noscript>
<p style='color:red'>oops your browser not supporting javascript
update/change the script settings and try..</p>
</noscript>
</body>