CS8651 Notes 005-2 Edubuzz360
CS8651 Notes 005-2 Edubuzz360
com
Method Description
https://play.google.com/store/apps/details?id=com.sss.edubuzz360
www.edubuzz360.com
Method Description
Write the JavaScript methods to retrieve the data and time based on the
computer locale.
<script>
</script>
OUTPUT
The current date is: Thu Jan 09 2020 20:35:39 GMT+0530 (India Standard Time)
Can you list the different methods defined in document and window object
5. of JavaScript.
https://play.google.com/store/apps/details?id=com.sss.edubuzz360
www.edubuzz360.com
Window Object
The window object represents an open window in a browser.
https://play.google.com/store/apps/details?id=com.sss.edubuzz360
www.edubuzz360.com
According to W3C - "The W3C Document Object Model (DOM) is a platform and
language-neutral interface that allows programs and scripts to dynamically access and
update the content, structure, and style of a document."
https://play.google.com/store/apps/details?id=com.sss.edubuzz360
www.edubuzz360.com
Method Description
https://play.google.com/store/apps/details?id=com.sss.edubuzz360
www.edubuzz360.com
Advantages of JavaScript
The merits of using JavaScript are −
Less server interaction − You can validate user input before sending the page off to the server. This
saves server traffic, which means less load on your server.
7.
Immediate feedback to the visitors − They don't have to wait for a page reload to see if they have
forgotten to enter something.
Increased interactivity − You can create interfaces that react when the user hovers over them with
a mouse or activates them via the keyboard.
Richer interfaces − You can use JavaScript to include such items as drag-and-drop components and
sliders to give a Rich Interface to your site visitors.
Client-side scripting (embedded scripts) is code that exists inside the client’s
HTML page. This code will be processed on the client machine and the HTML
page will NOT perform a PostBack to the web-server. Traditionally, client-side
scripting is used for page navigation, data validation and formatting. The language
used in this scripting is JavaScript. JavaScript is compatible and is able to run on
any internet browser.
8. 1. The user’s actions will result in an immediate response because they don’t require
a trip to the server.
The client-side script executes the code to the client side which is visible to the users while
a server-side script is executed in the server end which users cannot see.
https://play.google.com/store/apps/details?id=com.sss.edubuzz360
www.edubuzz360.com
catch ( e ) {
// Code to run if an exception occurs
[break;]
}
[ finally {
// Code that is always executed regardless of
// an exception occurring
}]
//-->
</script>
The try block must be followed by either exactly one catch block or one finally block
(or one of both). When an exception occurs in the try block, the exception is placed in e and
the
catch block is executed. The optional finally block executes unconditionally after try/catch.
Examples
Here is an example where we are trying to call a non-existing function which in turn is raising
an exception. Let us see how it behaves without try...catch−
<html>
<head>
<script type = "text/javascript">
<!--
function myFunc() {
var a = 100;
alert("Value of variable a is : " + a );
}
//-->
https://play.google.com/store/apps/details?id=com.sss.edubuzz360
www.edubuzz360.com
</script>
</head>
<body>
<p>Click the following to see the result:</p>
<form>
<input type = "button" value = "Click Me" onclick = "myFunc();" />
</form>
</body>
</html>
JavaScript Statements
Example
var x, y, z; // Statement 1
x = 5; // Statement 2
y = 6; // Statement 3
z = x + y; // Statement 4
<p id="demo"></p>
<script>
</script>
</body>
</html>
OUTPUT
https://play.google.com/store/apps/details?id=com.sss.edubuzz360
www.edubuzz360.com
JavaScript Statements
Hello Dolly.
<head>
<!--
function sayHello() {
alert("Hello World")
//-->
</script>
</head>
https://play.google.com/store/apps/details?id=com.sss.edubuzz360
www.edubuzz360.com
<body>
<form>
</form>
</body>
</html>
Example 2
<!doctype html>
<html>
<head>
<script>
function hov() {
var e = document.getElementById('hover');
e.style.display = 'none';
}
</script>
</head>
<body>
<div id="hover" onmouseover="hov()"
style="background-color:green;height:200px;width:200px;">
</div>
</body>
</html>
OUTPUT
Before mouse is taken over green square-
https://play.google.com/store/apps/details?id=com.sss.edubuzz360
www.edubuzz360.com
DHTML allows different scripting languages in a web page to change their variables, which
enhance the effects, looks and many others functions after the whole page have been fully
loaded or under a view process, or otherwise static HTML pages on the same.
DHTML is used to create interactive and animated web pages that are generated in real-time,
also known as dynamic web pages so that when such a page is accessed, the code within the
page is analyzed on the web server and the resulting HTML is sent to the client’s web
browser.
Key Features: Following are the some major key features of DHTML:
Tags and their properties can be changed using DHTML.
It is used for real-time positioning.
Dynamic fonts can be generated using DHTML.
It is also used for data binding.
14.
It makes a webpage dynamic and be used to create animations, games, applications along
with providing new ways of navigating through websites.
The functionality of a webpage is enhanced due to the usage of low-bandwidth effect by
DHTML.
DHTML also facilitates the use of methods, events, properties, and codes.
https://play.google.com/store/apps/details?id=com.sss.edubuzz360
www.edubuzz360.com
JSON Data Types. At the granular level, JSON consist of 6 data types. First four data types (string,
number, boolean and null) can be referred as simple data types. Other two data types (object and
array) can be referred as complex data types.
A JSON object is a key-value data format that is typically rendered in curly braces.
JSON object consist of curly braces ( { } ) at the either ends and have key-value pairs
inside the braces. Each key-value pair inside braces are separated by comma (, ).
JSON object looks something like this :
{
"key":"value",
"key":"value",
"key":"value",
16. }
Example for a JSON object :
{
"rollno":101",
"name":"Mayank",
"age":20,
}
For getting the value of any key from a Javascript object, we can use the values
as: object1.rollno
https://play.google.com/store/apps/details?id=com.sss.edubuzz360
www.edubuzz360.com
{
"name":"Peter parker",
"heroName": "Spiderman",
"friends" : ["Deadpool", "Hulk", "Wolverine"]
}
Accessing Array Values:
The Array values can be accessed using the index of each element in an Array.
EXAMPLE
<script>
var myObj, i, x = "";
myObj = {
"name":"John",
"age":30,
"cars":[ "Ford", "BMW", "Fiat" ]
};
for (i in myObj.cars) {
x += myObj.cars[i] + "<br>";
}
document.getElementById("demo").innerHTML = x;
</script>
OUTPUT
Ford
BMW
Fiat
How will you make a request with JSON?
https://play.google.com/store/apps/details?id=com.sss.edubuzz360
www.edubuzz360.com
The jQuery getJSON() method sends asynchronous http GET request to the server and retrieves
the data in JSON format by setting accepts header to application/json, text/javascript. This is
same as get() method, the only difference is that getJSON() method specifically retrieves JSON
data whereas get() method retrieves any type of data. It is like shortcut method to retrieve JSON
data. Syntax:
$.getJSON(url,[data],[callback]);
Parameter Description:
url: request url from which you want to retrieve the data
data: JSON data to be sent to the server as a query string
callback: function to be executed when request succeeds
The following example shows how to retrieve JSON data using getJSON() method.
<p></p>
OUTPUT
Steve
Define DDL and DML
DDL(Data Definition Language) : DDL or Data Definition Language actually consists of the
SQL commands that can be used to define the database schema. It simply deals with
descriptions of the database schema and is used to create and modify the structure of
database objects in the database.
20. DML(Data Manipulation Language) : The SQL commands that deals with the manipulation
of data present in the database belong to DML or Data Manipulation Language and this
includes most of the SQL statements.
https://play.google.com/store/apps/details?id=com.sss.edubuzz360
www.edubuzz360.com
Examples of DML:
INSERT – is used to insert data into a table.
UPDATE – is used to update existing data within a table.
DELETE – is used to delete records from a database table.
PART - B
Q.No Questions
i) Examine variables and data types in JavaScript.
Variables in JavaScript:
Variables in JavaScript are containers which hold reusable data. It is the basic unit of
storage in a program.
The value stored in a variable can be changed during program execution.
A variable is only a name given to a memory location, all the operations done on
the variable effects that memory location.
In JavaScript, all the variables must be declared before they can be used.
Examples
var x = 5; var price1 = 5;
var y = 6; var price2 = 6;
var z = x + y; var total = price1 + price2;
1.
var carName;
After the declaration, the variable has no value (technically it has the value of undefined).
carName = "Volvo";
You can also assign a value to the variable when you declare it:
https://play.google.com/store/apps/details?id=com.sss.edubuzz360
www.edubuzz360.com
EXAMPLE
<script> OUTPUT
var carName = "Volvo"; JavaScript Variables
document.getElementById("demo").innerHTML
= carName;
</script> Create a variable, assign a value to it,
and display it:
Volvo
JavaScript Operators
JavaScript Arithmetic Operators
Arithmetic operators are used to perform arithmetic on numbers:
+ Addition var x = 5;
var y = 2;
var z = x + y;
- Subtraction var x = 5;
var y = 2;
var z = x - y;
* Multiplication var x = 5;
var y = 2;
var z = x * y;
** Exponentiation
(ES2016)
/ Division
% Modulus (Division
Remainder)
++ Increment
-- Decrement
https://play.google.com/store/apps/details?id=com.sss.edubuzz360
www.edubuzz360.com
= x=y x=y
+= x += y x=x+y
-= x -= y x=x-y
*= x *= y x=x*y
/= x /= y x=x/y
%= x %= y x=x%y
**= x **= y x = x ** y
Assignment
var x = 10;
x += 5;
== equal to
!= not equal
https://play.google.com/store/apps/details?id=com.sss.edubuzz360
www.edubuzz360.com
? ternary operator
|| logical or
! logical not
Any numeric operand in the operation is converted into a 32 bit number. The result is converted
back to a JavaScript number.
Operator Description Example Same as Result Decimal
https://play.google.com/store/apps/details?id=com.sss.edubuzz360
www.edubuzz360.com
The examples above uses 4 bits unsigned examples. But JavaScript uses 32-bit signed numbers.
Because of this, in JavaScript, ~ 5 will not return 10. It will return -6.
~00000000000000000000000000000101 will return 11111111111111111111111111111010
"The W3C Document Object Model (DOM) is a platform and language-neutral interface that
allows programs and scripts to dynamically access and update the content, structure, and style of
a document."
https://play.google.com/store/apps/details?id=com.sss.edubuzz360
www.edubuzz360.com
With the object model, JavaScript gets all the power it needs to create dynamic HTML:
https://play.google.com/store/apps/details?id=com.sss.edubuzz360
www.edubuzz360.com
https://play.google.com/store/apps/details?id=com.sss.edubuzz360
www.edubuzz360.com
EXAMPLE
now.js
A JavaScript Regular Expression (or Regex) is a sequence of characters that we can utilize to
work effectively with strings. Using this syntax, we can:
https://play.google.com/store/apps/details?id=com.sss.edubuzz360
www.edubuzz360.com
JavaScript Loops
Loops are handy, if you want to run the same code over and over again, each
time with a different value.
<script>
var i;
for (i = 0; i < cars.length; i++) {
text += cars[i] + "<br>";
document.getElementById("demo").innerHTML = text;
</script>
OUTPUT
JavaScript For Loop
BMW
Volvo
Saab
Ford
Fiat
Audi
https://play.google.com/store/apps/details?id=com.sss.edubuzz360
www.edubuzz360.com
(i) Write a Java script to find the Prime number between 1 and 100.
<html>
<head>
<title>JavaScript Prime</title>
</head>
<body>
<script>
for (var limit = 1; limit <= 20; limit++) {
var a = false;
for (var i = 2; i <= limit; i++) {
if (limit%i===0 && i!==limit) {
a = true;
}
}
if (a === false) {
5. document.write("<br>"+limit);
}
}
</script>
</body>
</html>
function factorial(n) {
return (n != 1) ? n * factorial(n - 1) : 1;
}
https://play.google.com/store/apps/details?id=com.sss.edubuzz360
www.edubuzz360.com
<center>
<h1>Display Alert Message on Button Click Event.</h1>
<b>Click on button to display message: </b><br><br>
<input type="button" id="btnShowMsg" value="Click Me!"
onClick='showMessage()'/>
</center>
</body>
</html>
Result
- HTML DOM view the HTML document with a tree structure format and it consists of root
node and child nodes.
- The node-tree is being accessed using the tree formation and the structure in which the
elements get created.
- The contents that are used being modified or removed using the new elements and it can be
created within the limitations.
- The structure consists of a document that is the root and within it Root element <html> from
where the tree starts.
7.
- It consists of sub-elements like <head> and <body> and other text and attributes written in
the HTML format.
https://play.google.com/store/apps/details?id=com.sss.edubuzz360
www.edubuzz360.com
and also provides a mechanism by which some kind of action can be automatically taken (that
is, some code running) when the event occurs. For example in an airport when the runway is
clear for a plane to take off, a signal is communicated to the pilot, and as a result, they
commence piloting the plane.
In the case of the Web, events are fired inside the browser window, and tend to be
attached to a specific item that resides in it — this might be a single element, set of
elements, the HTML document loaded in the current tab, or the entire browser
window. There are a lot of different types of events that can occur, for example:
The user clicking the mouse over a certain element or hovering the cursor over a
certain element.
The user pressing a key on the keyboard.
The user resizing or closing the browser window.
A web page finishing loading.
A form being submitted.
A video being played, or paused, or finishing play.
An error occurring.
Each available event has an event handler, which is a block of code (usually a JavaScript
function that you as a programmer create) that will be run when the event fires. When such a
block of code is defined to be run in response to an event firing, we say we are registering an
event handler. Note that event handlers are sometimes called event listeners — they are
pretty much interchangeable for our purposes, although strictly speaking, they work together.
The listener listens out for the event happening, and the handler is the code that is run in
response to it happening.
In the following example, we have a single <button>, which when pressed, makes the
background change to a random color:
<button>Change color</button>
function random(number) {
return Math.floor(Math.random() * (number+1));
}
btn.onclick = function() {
const rndCol = 'rgb(' + random(255) + ',' + random(255) + ',' +
random(255) + ')';
document.body.style.backgroundColor = rndCol;
}
https://play.google.com/store/apps/details?id=com.sss.edubuzz360
www.edubuzz360.com
JavaScript Events
HTML events are "things" that happen to HTML elements.
When JavaScript is used in HTML pages, JavaScript can "react" on these events.
HTML Events
An HTML event can be something the browser does, or something a user does.
HTML allows event handler attributes, with JavaScript code, to be added to HTML elements.
In the following example, an onclick attribute (with code), is added to a <button> element:
Example
<button onclick="document.getElementById('demo').innerHTML = Date()">The time
is?</button>
OUTPUT
Sun Jan 12 2020 06:00:41 GMT+0530 (India Standard Time)
In the example above, the JavaScript code changes the content of the element with id="demo".
https://play.google.com/store/apps/details?id=com.sss.edubuzz360
www.edubuzz360.com
var s = date.getSeconds(); // 0 - 59
var session = "AM";
if(h == 0){
h = 12;
}
setTimeout(showTime, 1000);
showTime();
<html>
<head>
<title>Digital Clock</title>
<style>
#clock{
color:#F0F;
}
</style>
</head>
<body>
<script>
function digclock()
{
var d = new Date()
var t = d.toLocaleTimeString()
https://play.google.com/store/apps/details?id=com.sss.edubuzz360
www.edubuzz360.com
document.getElementById("clock").innerHTML = t
}
setInterval(function(){digclock()},1000)
</script>
Digital Clock
<div id="clock">
</div>
</body>
</html>
OUTPUT
Output :
Digital Clock
5:54:26 PM
Dynamic content, which allows the user to dynamically change Web page
content
10. Dynamic positioning of Web page elements
Dynamic style, which allows the user to change the Web page’s color, font,
size or content
EXAMPLE
<! DOCTYPE html PUBLIC "-//abc//DTD XHTML 1.1//EN"
"http://www.abc.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.abc.org/1999/xhtml">
<head>
<title>DHTML example</title>
<script type="text/JavaScript">
function greet_user()
{
var name=document.getElementById(“userName”).value;
https://play.google.com/store/apps/details?id=com.sss.edubuzz360
www.edubuzz360.com
if(name==””)
{
alert(“Welcome”+name);
}
else
{
alert(“Please provide User Name”)
}
}
</script>
</head>
<body>
<table border=”1” cellspacing=”3”>
<tr>
<td colspan=”2”><h6> Please Enter Your Name </h6></td>
</tr>
<tr>
<td><h4>User Name </h4></td>
<td><input type=”text” id=”userName” ></td>
</tr>
<tr>
<td colspan=”2”><input type=”button” value=”Submit”
onclick=”greet_user()”/>
</table>
</body>
</html>
(ii) Classify moving elements.
JSON is often used when data is sent from a server to a web page.
What is JSON?
* The JSON syntax is derived from JavaScript object notation syntax, but the
JSON format is text only. Code for reading and generating JSON data can be
written in any programming language.
https://play.google.com/store/apps/details?id=com.sss.edubuzz360
www.edubuzz360.com
JSON Example
JSON Example
{
"employees":[
{"firstName":"John", "lastName":"Doe"},
{"firstName":"Anna", "lastName":"Smith"},
{"firstName":"Peter", "lastName":"Jones"}
]
}
JSON Arrays
Arrays as JSON Objects
Example
[ "Ford", "BMW", "Fiat" ]
In JSON, array values must be of type string, number, object, array, boolean
or null.
In JavaScript, array values can be all of the above, plus any other valid
JavaScript expression, including functions, dates, and undefined.
12.
Example
{
"name":"John",
"age":30,
"cars":[ "Ford", "BMW", "Fiat" ]
}
https://play.google.com/store/apps/details?id=com.sss.edubuzz360
www.edubuzz360.com
Example
x = myObj.cars[0];
Example
for (i in myObj.cars) {
x += myObj.cars[i];
}
OUTPUT
Ford
BMW
Fiat
Analyze about Function files and Http Request with sample
program.
<!doctype html>
<title>Example</title>
<script>
// Store XMLHttpRequest and the JSON file location in variables
var xhr = new XMLHttpRequest();
var url = "https://www.quackit.com/json/tutorial/artists.txt";
https://play.google.com/store/apps/details?id=com.sss.edubuzz360
www.edubuzz360.com
// Function that formats the string with HTML tags, then outputs the result
function showArtists(data) {
var output = "<ul>"; // Open list
var i;
Summarize about
(i) SQL Data Definition Commands
Examples of Sql DDL commands are
SELECT – This SQL DML command select records or data from a table
INSERT – Insert data into a database table.
UPDATE – This SQL DML command will update existing records within a table
https://play.google.com/store/apps/details?id=com.sss.edubuzz360
www.edubuzz360.com
PART – C
Q.No Questions
Analyze the merits and demerits of DOM parser with neat example.
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
var str = "<root><customer name='John' address='Chicago'></customer></root>";
function CreateXMLDocument () {
var xmlDoc = null;
if (window.DOMParser) {
var parser = new DOMParser ();
xmlDoc = parser.parseFromString (str, "text/xml");
} else if (window.ActiveXObject) {
xmlDoc = new ActiveXObject ("Microsoft.XMLDOM");
xmlDoc.async = false;
1.
xmlDoc.loadXML (str);
}
Consider a java script and HTML to create a page with two panes.
The first pane (on left) should have a text area where HTML code
can bet typed by the user. The pane on the left should have a text
area where HTML code can be typed by the user. The pane on the
2. right side should display the preview of the HTML code typed by
the user, as it would be seen on the browser.
https://play.google.com/store/apps/details?id=com.sss.edubuzz360
www.edubuzz360.com
<body>
<p id="demo"></p>
</body>
</html>
OUTPUT
You can access the object values by using dot (.) notation:
Example
myObj = { "name":"John", "age":30, "car":null };
x = myObj.name;
You can also access the object values by using bracket ([]) notation:
Example
https://play.google.com/store/apps/details?id=com.sss.edubuzz360
www.edubuzz360.com
Looping an Object
You can loop through object properties by using the for-in loop:
Example
In a for-in loop, use the bracket notation to access the property values:
Example
<!DOCTYPE html>
<html>
<body>
<p id="demo"></p>
<script>
var myObj, x;
myObj = {"name":"John", "age":30, "car":null};
for (x in myObj) {
document.getElementById("demo").innerHTML += x + " " + myObj[x] + "<br>";
}
</script>
</body>
</html
OUTPUT
name John
age 30
car null
Example
myObj = {
"name":"John",
https://play.google.com/store/apps/details?id=com.sss.edubuzz360
www.edubuzz360.com
"age":30,
"cars": {
"car1":"Ford",
"car2":"BMW",
"car3":"Fiat"
}
}
You can access nested JSON objects by using the dot notation or bracket notation:
Example
x = myObj.cars.car2;
// or:
x = myObj.cars["car2"];
OUTPUT
BMW
BMW
https://play.google.com/store/apps/details?id=com.sss.edubuzz360