HTML CSS JS
HTML CSS JS
Web
Development
Unit-1
By
Dr. Parul Madan
5/11/2026 2
5/11/2026 3
Sample Footer Text 5/11/2026 4
Sample Footer Text 5/11/2026 5
Sample Footer Text 5/11/2026 6
FSWD
<!DOCTYPE html>
Just 15 characters!
The DOCTYPE tells the browser which type and version of document to
expect. This should be the last time the DOCTYPE is ever changed. From
now on, all future versions of HTML will use this same simplified declaration.
THE <HTML> ELEMENT
<html lang="en">
The lang attribute in the <html> element declares which language the page
content is in. Though not strictly required, it should always be specified, as it
can assist search engines and screen readers.
Each of the world’s major languages has a two-character code, e.g. Spanish = "es",
French = "fr", German = "de", Chinese = "zh", Arabic = "ar".
THE <HEAD> SECTION
<head>
<meta charset="utf-8">
<title>My First HTML5 Page</title>
<link rel="stylesheet" href="[Link]">
</head>
Notice the simplified character set declaration, the shorter CSS stylesheet link
text, and the removal of the trailing slashes for these two lines.
BASIC HTML5 WEB PAGE
Putting the prior sections together, and now adding the <body> section and
closing tags, we have our first complete web page in HTML5:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>My First HTML5 Page</title>
<link rel="stylesheet" href="[Link]">
</head>
<body>
<p>HTML5 is fun!</p>
</body>
</html>
• It helps in defining the page’s title, encoding, author, and viewport settings, etc.
These tags are not visible on the web page but play a vital role in structuring and
categorizing content for browsers and search engines.
What Is Crawlability?
• The crawlability of a webpage refers to how easily search engines (like Google) can
discover the page.
• Google discovers webpages through a process called crawling. It uses computer
programs called web crawlers (also called bots or spiders). These programs follow
links between pages to discover new or updated pages.
• Indexing usually follows crawling.
What Is Indexability?
• The indexability of a webpage means search engines (like Google) are able to add
the page to their index.
• The process of adding a webpage to an index is called indexing. It means Google
analyzes the page and its content and adds it to a database of billions of pages
(called the Google index).
<meta>
Syntax:
<meta attribute-name="value">
Attributes:
google Disables some optional Google features for your site, such as the sitelinks search box that <metaname="google" content="nositelinkssearchbox">
allows users to search within your site from Google, and the Google text-to-speech
services that enable users to hear your webpages. The supported values are
“nositelinkssearchbox” and “nopagereadaloud.”
google-site-verification Verifies the ownership of the website for Google Search Console. <meta name="google-site-verification"
content="+nxGUDJ4QpAZ5l9Bsjdi102tLVC21AIh5d1Nl23908v
VuFHs34=">
Content-Type and charset Specifies the content type and character set for the webpage. This is important for rendering <meta http-equiv="Content-Type" content="text/html;
non-ASCII characters correctly. charset=UTF-8">
refresh Tells the browser to automatically reload the page after a specified number of seconds. It can <meta http-equiv="refresh" content="5;
also be used to redirect the user to another URL after a certain time. url=[Link]
rating Indicates that a webpage contains explicit content. <meta name="rating" content="adult">
viewport Controls how the webpage is displayed on mobile devices <meta name="viewport" content="width=device-width, initial-
scale=1.0">
Examples
Define keywords for search engines:
Setting the viewport to make your website look good on all devices:
Setting Cookies
<meta http-equiv = "cookie" content = "userid = xyz; expires = Wednesday, 08 -Aug-15 23:59:59 GMT;" />
Content-Type
• This icon is actually displayed on the address bar, browser's tab, browser history,
bookmark bar, etc. The image of a favicon is in .ico file format. There are various
file formats, but .ico format is supported by all the browsers.
How to Create and insert a Favicon
Following are the steps for creating the favicon:
1. Click on the following URL, to create the favicon: [Link]
2. Once the favicon is successfully created, we can download the favicon by clicking on
the download favicon option.
3. After the downloading, a favicon with the name [Link] is available in the file system
drive.
4. Next, add a <link> element to your "[Link]" file, after the <title> element, like this:
<link rel="shortcut icon" href="[Link]" type="image/x-icon">
<link rel="shortcut icon" type="image/jpg" href="Favicon_Image_Location"/>
HTML Tag classification
Tags can be differentiated based on the purpose of usage. There are four types
1. Formatting Tags
• <font>, underline a part of a text <u>, making a text bold <b>…[Link] such as tables, div, span tags are used for layout and formatting of
content.
• Description, title, head, body etc are part of the page structure tags.
• They are part of the basic HTML page and does not directly affect the formatting of text or image.
3. Control Tags
• Form tags, Script tags, Radio buttons etc are part of the control tags.
• These tags are used to manage content or mange external scripts or libraries ( JQuery, Bootstrap)
4. Interactive Tags
• All form tags like input text box, drop -down list, radio buttons etc are used for interaction with a user.
Audio/Video Requires external plugins (like Flash) Native support with <audio> and <video>
Support
Semantics Limited semantic elements Rich semantic elements
like <article>, <nav>, <section>, <header>, <footer>
APIs and DOM Limited API support Extensive APIs (offline storage, drag-drop, etc.)
Graphics Limited; mainly through external plugins Canvas API for 2D/3D graphics
Storage Cookies for client-side storage Local storage, session storage
Accessibility Basic Improved with new semantic elements
Browser Consistent across modern browsers Varies; older browsers require polyfills
Compatibility
Mobile Support Limited Enhanced, with features for mobile optimization
Form Controls Basic controls New form controls like calendar, date, time, color picker
Scripting Heavily reliant on external scripts More integrated scripting capabilities
Structure Less emphasis on document structure Strong focus on document structure
• Image
• List
• Table
• Form
• Frame
• Audio
• Video
• Canvas
• Map
• _self - Default. Opens the document in the same window/tab as it was clicked
Image as h yperlink
• <a href="[Link]">
<img src="[Link]" alt="HTML tutorial" style="width:42px ;heigh t:42px;">
</a>
<a href="#intro“>Introduction</a>
• placeholder.
• autofocus.
• autocomplete.
• required.
• 2. Wildcard ( . ) The Regular expression .* will tell the computer that any character can be used
any number of times.
• 3. Optional character ( ? )
• 6. Character Classes
• </map>
• <source src="movie.mp4" type="video/mp4"> height It is used to set the height of the video player.
• Your browser does not support the html video tag. poster It specifies the image which is displayed on
the screen when the video is not played.
• selector::pseudo-element {
property: value;
}
• p::first-line {
color: #ff0000;
font-variant: small-caps;
}
• p::first-letter {
color: #ff0000;
font-size: xx-large;
}
::before p::before Insert something before the content of each <p> element
:first-child p:first-child Selects every <p> elements that is the first child of its parent
:first-of-type p:first-of-type Selects every <p> element that is the first <p> element of its parent
:in-range input:in-range Selects <input> elements with a value within a specified range
:lang(language) p:lang(it) Selects every <p> element with a lang attribute value starting with "it"
:last-child p:last-child Selects every <p> elements that is the last child of its parent
:last-of-type p:last-of-type Selects every <p> element that is the last <p> element of its parent
:nth-child(n) p:nth-child(2) Selects every <p> element that is the second child of its parent
:nth-last-child(n) p:nth-last-child(2) Selects every <p> element that is the second child of its parent, counting from the last child
:nth-last-of-type(n) p:nth-last-of-type(2) Selects every <p> element that is the second <p> element of its parent, counting from the last child
:nth-of-type(n) p:nth-of-type(2) Selects every <p> element that is the second <p> element of its parent
:only-of-type p:only-of-type Selects every <p> element that is the only <p> element of its parent
:only-child p:only-child Selects every <p> element that is the only child of its parent
:out-of-range input:out-of-range Selects <input> elements with a value outside a specified range
:target #news:target Selects the current active #news element (clicked on a URL containing that anchor name)
5/11/2026 54
:visited a:visited Selects all visited links
BOX Model
• <!DOCTYPE address [
• ]>
• <address>
• <name>
• <first>Rohit</first>
• <last>Sharma</last>
• </name>
• <email>sharmarohit@[Link]</email>
• <phone>9876543210</phone>
• <birthday>
• <year>1987</year>
• <month>June</month>
• <day>23</day>
• </birthday>
• </address>
• [Link] -vs-html4/
• [Link] -installation
• [Link] -create-react-app
• [Link] -regular-expressions/
• [Link]
• [Link] -keyframe-css-animation-examples-and-code/
• [Link] -keyframe-css-animation-examples-and-code/
• [Link]
• [Link] -tutorial/css-keyframes
• [Link]
Cascading Style Sheets, fondly referred to as CSS, is a simple design language intended to
simplify the process of making web pages presentable.
some of the key advantages of learning CSS:
Create Stunning Web site - CSS handles the look and feel part of a web page. Using CSS,
you can control the color of the text, the style of fonts, the spacing between paragraphs, how
columns are sized and laid out, what background images or colors are used, layout
designs,variations in display for different devices and screen sizes as well as a variety of
other effects.
Become a web designer - If you want to start a carrer as a professional web designer, HTML
and CSS designing is a must skill.
Control web - CSS is easy to learn and understand but it provides powerful control over the
presentation of an HTML document. Most commonly, CSS is combined with the markup
languages HTML or XHTML.
APPLICATIONS OF CSS
CSS saves time - You can write CSS once and then reuse same sheet in multiple HTML pages. You
can define a style for each HTML element and apply it to as many Web pages as you want.
Pages load faster - If you are using CSS, you do not need to write HTML tag attributes every time.
Just write one CSS rule of a tag and apply it to all the occurrences of that tag. So less code means
faster download times.
Easy maintenance - To make a global change, simply change the style, and all elements in all the
web pages will be updated automatically.
Superior styles to HTML - CSS has a much wider array of attributes than HTML, so you can give
a far better look to your HTML page in comparison to HTML attributes.
Multiple Device Compatibility - Style sheets allow content to be optimized for more than one type
of device. By using the same HTML document, different versions of a website can be presented for
handheld devices such as PDAs and cell phones or for printing.
Global web standards - Now HTML attributes are being deprecated and it is being recommended
to use CSS. So, its a good idea to start using CSS in all the HTML pages to make them compatible to
future browsers.
PREREQUISITES
Cascading Style Sheet(CSS) is used to set the style in web pages that contain
HTML elements. It sets the background color, font-size, font-family, color
etc., property of elements on a web page.
There are three types of CSS which are given below:
Inline CSS
Internal or Embedded CSS
External CSS
INLINE CSS: INLINE CSS CONTAINS THE CSS PROPERTY IN THE BODY SECTION ATTACH ED WITH ELEMENT IS KNOWN AS
INLINE CSS. THIS KIND OF STYLE IS SPECIFIED WITHIN AN HTML TAG U SING THE STYLE ATTRIBUTE.
<body>
<p style = "color:#009900; font-size:50px;
font-style:italic; text-align:center;">
GRAPHIC ERA
</p>
</body>
</html>
INTERNAL OR EMBEDDED CSS: THIS CAN BE USED WHEN A SINGLE HTML DOCUMENT MUST BE STYLED UNIQUELY. THE CSS RULE
SET SHOULD BE WITHIN THE HTML FILE IN THE HEAD SECTION I.E. THE CSS IS EMBEDDED WITHIN THE HTML FILE.
.GEU {
font-style: bold;
E.g.
font-size:20px;
<!DOCTYPE html>
}
<html>
<head> </style>
} </div>
</div>
</body>
EXTERNAL CSS: EXTERNAL CSS CONTAINS SEPARATE CSS FILE WHICH CONTAINS ONLY ST YLE PROPERTY WITH
THE HELP OF TAG ATTRIBUTES (FOR EXAMPLE CLASS, ID, HEADING ETC.) . CSS PROPERTY WRITTEN IN A SEPARATE
FILE WITH .CSS EXTENSION AND SHOULD BE LINKED TO THE HTML DOCUME NT USING LINK TAG. THIS MEANS THAT
FOR EACH ELEMENT, STYLE CAN BE SET ONLY ONCE AND THAT WILL BE AP PLIED ACROSS WEB PAGES.
body { <!DOCTYPE html>
<html>
background-color:powderblue;
<head>
}
<link rel="stylesheet" href=“[Link]"/>
.main {
</head>
text-align:center;
} <body>
font-style:bold; </html>
font-size:20px; link tag is used to link the external style sheet with the html webpage.
href attribute is used to specify the location of the external style sheet file.
}
PROPERTIES OF CSS
Properties of CSS: Inline CSS has the highest priority, then comes Internal/Embedded
followed by External CSS which has the least priority. Multiple style sheets can be
defined on one page. If for an HTML tag, styles are defined in multiple style sheets
then the below order will be followed.
As Inline has the highest priority, any styles that are defined in the internal and
external style sheets are overridden by Inline styles.
Internal or Embedded stands second in the priority list and overrides the styles in the
external style sheet.
External style sheets have the least priority. If there are no styles defined either in
inline or internal style sheet then external style sheet rules are applied for the HTML
tags.
CSS SELECTORS
<body>
h1#black {
<p class="important">Note that this is an important paragraph. </p> all level 2 headings will be displayed in black color when those
headings will lie with in tags having id attribute set to black.
#black h2 {
</body>
color: #000000;
</html>
}
[Link] SELECTOR: YOU CAN DEFINE STYLE RULES BASED ON THE CLASS ATTRIBUTE OF THE ELEMENTS.
ALL THE ELEMENTS HAVING THAT CLASS WILL BE FORMATTED ACCORDING T O THE DEFINED RULE.
<html>
This rule renders the content in black for every
<head>
element with class attribute set to black in our
<style>
document. You can make it a bit more particular.
.intro { For example −
color: blue;
}
.black {
[Link] {
color: #000000;
font-style:bold;
} }
</style>
</head>
</html>
3. ELEMENT SELECTOR
<html>
<head>
<style>
p{
color: blue;
</style>
</head>
<body>
<p>A paragraph.</p>
</body>
</html>
4. UNIVERSAL SELECTORS
<html>
<head>
<style>
*{
color: blue;
</style>
</head>
<body>
<p>A paragraph.</p>
</body>
</html>
[Link] SELECTORS
<html>
</head>
margin-bottom:
<body> 1em;
<h1 id="intro">Header 1</h1>
<p>A paragraph.</p>
}
</body>
[Link] ATTRIBUTE SELECTORS
You can also apply styles to HTML elements with particular attributes. The style rule below will match all
the input elements having a type attribute with a value of text −
input[type = "text"] {
color: #000000;
}
The advantage to this method is that the <input type = "submit" /> element is unaffected, and the color
applied only to the desired text fields.
There are following rules applied to attribute selector.
p[lang] − Selects all paragraph elements with a lang attribute.
p[lang="fr"] − Selects all paragraph elements whose lang attribute has a value of exactly "fr".
p[lang~="fr"] − Selects all paragraph elements whose lang attribute contains the word "fr".
p[lang|="en"] − Selects all paragraph elements whose lang attribute contains values that are exactly
"en", or begin with "en-".
7. DESCENDANT SELECTORS
Suppose you want to apply a style rule to a particular element only when it lies
inside a particular element. As given in the following example, style rule will
apply to <em> element only when it lies inside <ul> tag.
ul em {
color: #000000;
}
8. CHILD SELECTORS
You have seen the descendant selectors. There is one more type of selector,
which is very similar to descendants but have different functionality. Consider
the following example −
body > p {
color: #000000;
}
CSS TEXT COLOR
<h1 style="color:Tomato;">Hello World</h1>
<p style="color:DodgerBlue;">Lorem ipsum...</p>
<p style="color:MediumSeaGreen;">Ut wisi enim...</p>
CSS Border Color
<h1 style="border:2px solid Tomato;">Hello World</h1>
<h1 style="border:2px solid DodgerBlue;">Hello World</h1>
<h1 style="border:2px solid Violet;">Hello World</h1>
CSS Color Values
rgb(255, 99, 71)
#ff6347
hsl(9, 100%, 64%)
rgba(255, 99, 71, 0.5)
hsla(9, 100%, 64%, 0.5)
CSS BACKGROUNDS
The CSS background properties are used to define the background effects for
elements.
background-color
background-image
background-repeat
background-attachment
background-position
BACKGROUND COLOR
h1 {
background-color: green;
}
div {
background-color: lightblue;
}
p{
background-color: yellow;
}
Opacity / Transparency: It can take a value from 0.0 - 1.0
div {
background-color: green;
opacity: 0.3;
}
BACKGROUND IMAGE
The background-image property specifies an image to use as the background of an element.
<!DOCTYPE html>
<html>
<head>
<style>
body {
background-image: url("[Link]");
}
</style>
</head>
<body>
<h1>Hello World!</h1>
</body>
</html>
CSS BACKGROUND-REPEAT
background-color
background-image
background-repeat
background-attachment
background-position
CSS BORDER PROPERTIES
The CSS border properties allow you to specify the style, width, and color of an element's border.
CSS Border Style:
The border property is a shorthand property for the following individual border
properties:
border-width
border-style (required)
border-color
e.g.
p{
border: 5px solid red;
}
CSS MARGINS
The CSS margin properties are used to create space around elements, outside of any
defined borders.
CSS has properties for specifying the margin for each side of an element:
margin-top
margin-right
margin-bottom
margin-left
MARGIN - SHORTHAND PROPERTY
The margin property is a shorthand property for the following individual margin properties:
margin: 25px 50px 75px 100px;
Top margin is 25px
right margin is 50px
bottom margin is 75px
left margin is 100px
margin: 25px 50px 75px;
top margin is 25px
right and left margins are 50px
bottom margin is 75px
margin: 25px 50px;top and bottom margins are 25px
right and left margins are 50px
margin: 25px;
all four margins are 25px
CSS MARGIN COLLAPSE
Top and bottom margins of elements are sometimes collapsed into a single margin that is
equal to the largest of the two margins.
This does not happen on left and right margins. Only top and bottom margins.
h1 {
margin: 0 0 50px 0;
}
h2 {
margin: 20px 0 0 0;
}
common sense would seem to suggest that the vertical margin between the <h1> and the <h2>
would be a total of 70px (50px + 20px). But due to margin collapse, the actual margin ends
up being 50px.
CSS PADDING
The CSS padding properties are used to generate space around an element's content, inside of any defined
borders.
CSS has properties for specifying the padding for each side of an element:
padding-top
padding-right
padding-bottom
padding-left
e.g.
div {
padding-top: 50px;
padding-right: 30px;
padding-bottom: 50px;
padding-left: 80px;
}
CSS HEIGHT AND WIDTH VALUES
The height and width properties are used to set the height and width of an element.
The height and width properties do not include padding, borders, or margins. It sets the
height/width of the area inside the padding, border, and margin of the element.
The height and width properties may have the following values:
auto - This is default. The browser calculates the height and width
length - Defines the height/width in px, cm etc.
% - Defines the height/width in percent of the containing block
initial - Sets the height/width to its default value
inherit - The height/width will be inherited from its parent value
div {
height: 200px;
width: 50%;
background-color: powderblue;
}
THE CSS BOX MODEL
All HTML elements can be considered as boxes. In CSS, the term "box model" is used when talking about
design and layout. The CSS box model is essentially a box that wraps around every HTML element. It consists
of: margins, borders, padding, and the actual content. The image below illustrates the box model:
Explanation of the different parts:
Content - The content of the box, where text and images appear
Padding - Clears an area around the content. The padding is transparent
Border - A border that goes around the padding and content
Margin - Clears an area outside the border. The margin is transparent
CSS OUTLINE
An outline is a line that is drawn around elements, OUTSIDE the borders, to make the element
"stand out".
CSS has the following outline properties:
outline-style
outline-color
outline-width
outline-offset
outline
CSS OUTLINE SHORTHAND
The outline property is a shorthand property for setting the following individual
outline properties:
outline-width
outline-style (required)
outline-color
CSS OUTLINE OFFSET
The outline-offset property adds space between an outline and the edge/border of
an element. The space between an element and its outline is transparent.
p{
margin: 30px;
border: 1px solid black;
outline: 1px solid red;
outline-offset: 15px;
}
CSS LAYOUT - FLOAT AND CLEAR
</body>
</html>
:LANG
<html>
<head>
<style type = "text/css">
<body>
<p>...<q lang = "en">A quote in a paragraph</q>...</p>
</body>
</html>
CSS PSEUDO ELEMENTS
::BEFORE (WILL PLACE SOME CONTENT BEFORE ELEMENT SELECTOR)
<!DOCTYPE html>
<html>
<head>
<style>
p::before {
content: "this is it";
background-color: yellow;
color: red;
font-weight: bold;
}
</style>
</head>
<body>
</body>
</html>
::SELECTION
<!DOCTYPE html>
<html>
<head>
<style>
::selection {
color: red;
background: yellow;
}
</style>
</head>
<body>
<p>This is a paragraph.</p>
<div>This is some text in a div element.</div>
</body>
</html>
::FIRST-LETTER
<!DOCTYPE html>
<html>
<head>
<style>
p::first-letter {
font-size: 200%;
color: #8A2BE2;
}
</style>
</head>
<body>
<h1>Welcome to My Homepage</h1>
<p>My name is Donald.</p>
<p>I live in Duckburg.</p>
<p>My best friend is Mickey.</p>
Introduction to JSON
By:
Dr. PARUL MADAN
CSE GEU
Introduction to the JavaScript Object Notation
(JSON)
Standard Structure: As we have seen so far that JSON objects are having a
standard structure that makes developers job easy to read and write code,
because they know what to expect from JSON.
Light weight: When working with AJAX, it is important to load the data
quickly and asynchronously without requesting the page re-load. Since JSON is
light weighted, it becomes easier to get and load the requested data quickly.
Scalable: JSON is language independent, which means it can work well with
most of the modern programming language. Let’s say if we need to change the
server side language, in that case it would be easier for us to go ahead with that
change as JSON structure is same for all the languages.
JSON VS XML
JSON is lightweight thus simple to read and write. XML is less simple than JSON.
JSON supports array data structure. XML doesn't support array data structure.
JSON files are more human readable XML files are less human readable.
JSON has no display capabilities . XML provides the capability to display data because it is a
markup language
Provides scalar data types and the ability to express Does not provide any notion of data types. One must rely
structured data through arrays and objects. on XML Schema for adding type information.
Null The Null value represents that the associatedvariable doesn't have any value
Object it is collection of key-value pairs separated by ycbommas and enclosed in curly brackets
JSON Objects {
"employee":{ "name":"John", "age":30, "city":"New
York" }
}
JSON Arrays {
"employees":[ "John", "Anna", "Peter" ]
}
Eg:
myObj = { "name":"John", "age":30, "car":null };
Accessing Object Values:
x = [Link]; or x = myObj["name"];
Looping an Object:
for (x in myObj) {
[Link]("demo").innerHTML += x;//or myObj[x];
}
Nested JSON Objects
[Link]("<hr />");
[Link]([Link] + " programming language can be
studied " + "from book written by " + [Link]);
[Link]("<hr />");
</script>
</head>
<body>
</body>
</html>
Live Demo
<html>
<head>
<title>Creation of array object in javascript using JSON</title>
<script language = "javascript" >
[Link]("<h2>JSON array object</h2>");
var books = { "Pascal" : [
{ "Name" : "Pascal Made Simple", "price" : 700 },
{ "Name" : "Guide to Pascal", "price" : 400 }],
"Scala" : [
{ "Name" : "Scala for the Impatient", "price" : 1000 },
{ "Name" : "Scala in Depth", "price" : 1300 }]
}
var i = 0
[Link]("<table border = '2'><tr>");
for(i = 0;i<[Link];i++) {
[Link]("<td>");
[Link]("<table border = '1' width = 100 >");
[Link]("<tr><td><b>Name</b></td><td width = 50>" + [Link][i].Name+"</td></tr>");
[Link]("<tr><td><b>Price</b></td><td width = 50>" + [Link][i].price +"</td></tr>");
[Link]("</table>");
[Link]("</td>");
}
for(i = 0;i<[Link];i++) {
[Link]("<td>");
[Link]("<table border = '1' width = 100 >");
[Link]("<tr><td><b>Name</b></td><td width = 50>" + [Link][i].Name+"</td></tr>");
[Link]("<tr><td><b>Price</b></td><td width = 50>" + [Link][i].price+"</td></tr>");
[Link]("</table>");
[Link]("</td>");
}
[Link]("</tr></table>");
</script>
</head>
<body>
Parsing JSON
Use the JavaScript function [Link]() to convert text into a JavaScript object:
<p id="demo"></p>
<script>
var txt = '{"name":"John", "age":30, "city":"New York"}'
var obj = [Link](txt);
[Link]("demo").innerHTML = [Link] + ", " + [Link];
</script>
</body>
</html>
JSON From the Server
<script> Json_demo.json
var xmlhttp = new XMLHttpRequest(); { "name":"John", "age":31,
[Link] = function() { "pets":[
if ([Link] == 4 && [Link] == 200) { { "animal":"dog", "name":"Fido" },
var myObj = [Link]([Link]); { "animal":"cat", "name":"Felix" },
[Link]("demo").innerHTML = {"animal":"hamster","name":"Lightning" } ]
[Link]; }
}
};
[Link]("GET", "json_demo.json", true);
[Link]();
</script>
The JSON returned from the server is an
array:
var xmlhttp = new XMLHttpRequest();
[Link] = function() { json_demo_array.json:
if ([Link] == 4 && [Link] == 200) {
var myArr = [Link]([Link]); [ "Ford", "BMW", "Audi", "Fiat" ]
[Link]("demo").innerHTML = myArr[0];
}
};
[Link]("GET", "json_demo_array.json", true);
[Link]();
Parsing Dates:
For eg:
<script>
var obj = { name: "John", age: 30, city: "New York" };
var myJSON = [Link](obj);
[Link]("demo").innerHTML = myJSON;
</script>
Stringify a JavaScript Array
Eg:
var arr = [ "John", "Peter", "Sally", "Jane" ];
var myJSON = [Link](arr);
[Link]("demo").innerHTML = myJSON;
Stringify Dates
var obj = { name: "John", today: new Date(), city : "New York" };
var myJSON = [Link](obj);
[Link]("demo").innerHTML = myJSON;
Stringify Functions
var obj = { name: "John", age: function () {return 30;}, city: "New
York"};
var myJSON = [Link](obj);
[Link]("demo").innerHTML = myJSON;
JSON PHP
Objects in PHP can be converted into JSON by using the PHP function json_encode():
For eg: demo1_file.php
<?php
class Emp {
public $name = "";
public $age = "";
public $city = "";
}
$myObj=new Emp();
$myObj->name = "John";
$myObj->age = 30;
$myObj->city = "New York";
$myJSON = json_encode($myObj);
echo $myJSON;
?>
The Client JavaScript
echo json_encode($outp);
?>
The JavaScript XmlHttpRequest and Web APIs
• The JavaScript XMLHttpRequest is the client making the requests, and the web API is the server sending the
responses.
• XMLHttpRequest is anAPI that can be used by JavaScript, jQuery that provides client functionality for transferring
data between a client and a server. It provides an easy way to retrieve data from a URL without having to do a full
page refresh.
Web API
A web API is a set of instructions and standards for interacting with a service over HTTP. That interaction can include
create, read, update, and delete (CRUD) operations and the web API will have a reference outlining these instructions
and standards.
JSON weather data:
{
"dt": 1433383200,
"temp": {"day": 293.5, "min": 293.5, "max": 293.5, "night": 293.5, "eve": 293.5, "morn":
293.5},
"pressure": 1015.06,
"humidity": 98,
"weather": [{"id": 802, "main": "Clouds", "description": "scattered clouds","icon": "03n"}],
"speed": 2.86,
"deg": 134,
"clouds": 44
}
The JSON weather data example can be “read” by any code capable of parsing JSON. This JSON
resource can be requested with a URL.
For example, a news web page might include a sidebar with real-time weather data. While you’re
reading your news article, code in the back‐ground could asynchronously update the weather display
every 60 seconds. This operation would not require the page to reload and interrupting any other
events
JavaScript Regular Expressions
A regular expression is a sequence of characters that forms a
search pattern.
The search pattern can be used for text search and text replace
operations.
When you search for data in a text, you can use this search
pattern to describe what you are searching for.
Syntax
/pattern/modifiers;
Modifiers
Modifiers are used to perform case-insensitive and global searches:
Modifier Description
g Perform a global match (find all matches rather than stopping
after the first match)
i Perform case-insensitive matching
m Perform multiline matching
Example
str = "Please visit Microsoft and Microsoft!";
var n = [Link](/Microsoft/g, "WIPRO");
To replace all matches, with a /g flag (global
match)and an /i flag (insensitive):
var n = [Link](/Microsoft/gi, "WIPRO");
<!DOCTYPE html>
<html>
<body>
<p> New String is</p>
<p id="demo"></p>
<script>
str="web programming web developer web design"
var nstr = [Link](/web/gi);
[Link]("demo").innerHTML =
nstr;
</script>
</body>
</html>
OutPut
New String is
web,web,web
RegExp Object Methods
1 exec()
2 test()
3 toString()
JavaScript - RegExp exec() Method
Description
The exec() Method in JavaScript is used to test for
match in a string. If there is a match this method
returns the first match else it returns NULL.
<!DOCTYPE html>
<html>
<body>
<p> New String is</p>
<p id="demo"></p>
<script>
str="web programming web developer web design"
var nstr = /web/.exec(str);
[Link]("demo").innerHTML = nstr;
</script>
</body>
</html>
Output
New String is
web
<!DOCTYPE html>
<html>
<body>
<p> New String is</p>
<p id="demo"></p>
<script>
str="web programming web developer web design"
var reg = new RegExp("web");
var nstr = [Link](str);
[Link]("demo").innerHTML = nstr;
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<p> New String is</p>
<p id="demo"></p>
<script>
let str="web programming web developer web design"
let pattern=/web/
var nstr = [Link](str);
[Link]("demo").innerHTML = nstr;
</script>
</body>
</html>
JavaScript RegExp toString() method
Definition and Usage
The toString() method returns the string
value of the regular expression.
<!DOCTYPE html>
<html>
<body>
<p> New String is</p>
<p id="demo"></p>
<script>
str="web programming web developer web design"
var reg = new RegExp("web");
var nstr = [Link](str);
[Link]("demo").innerHTML =
nstr;
</script>
</body>
</html>
Output
New String is
/web/
Brackets [ ]
Brackets are used to find a range of characters:
Expression Description
[abc] Find any character between the
brackets
[^abc] Find any character NOT between the
brackets
[0-9] Find any character between the
brackets (any digit)
[^0-9] Find any character NOT between the
brackets (any non-digit)
(x|y) Find any of the alternatives specified
<!DOCTYPE html>
<html>
<body>
<p> New String is</p>
<p id="demo"></p>
<script>
str="web programming web developer meb design jeb Design"
Example
output
New String is
sengupta,dasgupta
Quantifiers
Quantifier Description
n+ Matches any string that contains at least one n
n* Matches any string that contains zero or more
occurrences of n
n? Matches any string that contains zero or one
occurrences of n
n{X} Matches any string that contains a
sequence of X n's
n{X,Y} Matches any string that contains a
sequence of X to Y n's
n{X,} Matches any string that contains a
sequence of at least X n's
n$ Matches any string with n at the end of it
^n Matches any string with n at the beginning of it
?=n Matches any string that is followed by a
specific string n
?!n Matches any string that is not followed by a
specific string n
<!DOCTYPE html>
<html>
<body>
<p> New String is</p>
<p id="demo"></p>
<script>
str="aggarwal agarwal agggarwal aarwal are web
developer"
var nstr = [Link](/g+/g);
[Link]("demo").innerHTML = nstr;
</script>
</body>
</html>
Output
New String is
gg,g,ggg
<!DOCTYPE html>
<html>
<body>
<p> New String is</p>
<p id="demo"></p>
<script>
str="aggarwal agarwal Agggarwal aarwal are web
developer"
var nstr = [Link](/[aA]g+arwal/g);
[Link]("demo").innerHTML = nstr;
</script>
</body>
</html>
Output
New String is
aggarwal,agarwal,Agggarwal
n* Matches any string that contains zero or
more occurrences of n
<script>
str="aggarwal agarwal Agggarwal aarwal are web
developer"
var nstr = [Link](/[aA]g*arwal/g);
[Link]("demo").innerHTML =
nstr;
</script>
Output
New String is
aggarwal,agarwal,Agggarwal,aarwal
Output
New String is
5009,4500,5678
Output
New String is
5009,4500,234,5678
New String is
hello,welco,java,scrip,progr
n{X,} Matches any string that contains a
sequence of at least X n's
<script>
New String is
hello,welcome,java,script,program
Output
New String is
sandeep mandeep amandeep rajdeep
^n Matches any string with n at the beginning of it
<!DOCTYPE html>
<html>
<body>
<p> New String is</p>
<p id="demo"></p>
<script>
str="sandeep sanjay varun vijay amarsan saneev sanjiv"
let astr=[Link](" ")
result=""
for(i=0;i<[Link];i++)
{
nstr = astr[i].match(/^san[a-z]*/);
if(nstr!=null)
result=result+nstr+" "
}
[Link]("demo").innerHTML = result
</script>
</body>
</html>
Output
New String is
sandeep sanjay saneev sanjiv