WEB DESIGNING VNSGU UNIT 4 JAVASCRIPT OBJECTS

divyapatel123440 28 views 53 slides Jun 01, 2024
Slide 1
Slide 1 of 53
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35
Slide 36
36
Slide 37
37
Slide 38
38
Slide 39
39
Slide 40
40
Slide 41
41
Slide 42
42
Slide 43
43
Slide 44
44
Slide 45
45
Slide 46
46
Slide 47
47
Slide 48
48
Slide 49
49
Slide 50
50
Slide 51
51
Slide 52
52
Slide 53
53

About This Presentation

WEB DESIGNING VNSGU UNIT 4 JAVASCRIPT OBJECTS


Slide Content

UNIT4:JAVASCRIPT OBJECTS
1.Creatingobject:(Byobjectliteral,Bycreatinginstanceof
Object,Byusinganobjectconstructor)
2.Dateobject:
1.Dateconstructor:Date(),Date(milliseconds),
Date(dateString),Date(year,month,day,hours,minutes,
seconds,milliseconds)
2.DateMethods:getDate(),getDay(),getMonth(),
getHours(),
setDate,setMonth(),setDay(),toString()

3.DocumentObjectModel(DOM):
1. DOMconcepts
2. DOMproperties
3. DOMmethods:
•write(),
•writeln(),
•getElementById(),
•getElementsByName()

4.1 Document Object Model (DOM):
1. DOMconcepts
2. DOMproperties
3. DOMmethods:
•write(),
•writeln(),
•getElementById(),
•getElementsByName()

4.1.1DOMconcepts
•TheDocumentObjectModel(DOM)isan
applicationprogramminginterface(API)for
manipulatingHTMLdocuments(add,remove,
andmodifypartsofanHTMLdocument).
•Itdefinesthelogicalstructureofthedocuments
intheformofatreeofobjectswhereeach
HTMLelementisrepresentedasanode.
•Whenawebpageisloaded,thebrowsercreates
aDocumentObjectModelofthepage.

<!doctype html>
<html>
<head>
<title>My title</title>
</head>
<body>
<h1> My header</h1>
<a href=“link.html”>My link</a>
</body>
</html>

TheHTMLDOMTreeofObjects

Withtheobjectmodel,JavaScriptgetsallthepowerit
needstocreatedynamicHTML:
•JavaScriptcanchangealltheHTMLelementsinthe
page
•JavaScriptcanchangealltheHTMLattributesinthe
page
•JavaScriptcanchangealltheCSSstylesinthepage
•JavaScriptcanremoveexistingHTMLelementsand
attributes
•JavaScriptcanaddnewHTMLelementsand
attributes
•JavaScriptcanreacttoallexistingHTMLeventsin
thepage
•JavaScriptcancreatenewHTMLeventsinthepage

•NOTE:IntheDOM,allHTMLelementsare
definedasObjects.Soitwillhaveboth
propertyandmethod.
•Thedocumentobjectrepresentsyourweb
page.
•IfyouwanttoaccessanyelementinanHTML
page,youalwaysstartwithaccessingthe
documentobject.

4.1.2DOMproperties
□DOMpropertiesarevalues(ofHTMLElements)
thatyoucansetorchange.
□DOMpropertiesareaccessedusingthe
“document” object.
□Syntax:
■document.property_name

Properties Description
links
refers to all <a> and <area> elements that have a “href” attributes
anchors refers to all <a> elements that have “name” attributes
scripts refers to all <script> elements
images refers to all <img> elements
doctype Specify the document’s type
head
Refers to all < head> elements
Forms
refers to all <form> elements
cookie returns all name/value pairs of cookies in the document
Domain refersdomainnameoftheserver
lastModifiedget the date and time of the last updated document
Title
refers to <title> of the document
URL specify the URL of the Document

<html>
<body>
<p id="demo">Hello Javascript</p>
<script>
document.getElementById("demo").innerHTM
L = "Hello World!";
</script>
</body>
</html>

TheinnerHTMLpropertyisusefulforgetting
orreplacingthecontentofHTMLelements.

4.1.3DOMmethods:
DOMmethodsareactionsyoucan
perform(onHTMLElements).
□write()
□writeln()
□getElementById()
□getElementsByName()

write()
□WritesometextdirectlytotheHTML
document.
□Syntax:
■document.write(exp1, exp2, exp3, ...)

Example:
<script>
document.write(“VNSGU”);
</script>

writeln()
□Thewriteln()methodisidenticaltothe
document.write()method,withthe
additionofwritinganewlinecharacter
aftereachstatement.

Example:
<script>
document.writeln(“VNSGU”);
document.writeln(“SURAT”);
</script>

getElementById()
□ThegetElementById()methodreturnsthe
elementthathastheIDattributewiththe
specifiedvalue.
□Syntax:
■document.getElementById(elementID)

Example:
<html>
<head>
<script>
function test()
{
var s=document.getElementById("txt").value;
document.write(s);
}
</script>
</head>
<body>
Enter Your Name :<input type="text" id="txt"> <br>
<input type="button" value="Click Here" onclick="test();">
</body>
</html>

getElementsByName()
□ThegetElementsByName()methodreturns
acollectionofallelementsinthe
documentwiththespecifiedname.
□TheHTMLCollectionobjectrepresentsa
collectionofnodes.Thenodescanbe
accessedbyindexnumbers.Theindexstarts
at0.
□Syntax:
■document.getElementsByName(name)

Example:
<html>
<head>
<script>
function test()
{
var s1=document.getElementsByName("txt")[0].value;
var s2=document.getElementsByName("txt")[1].value;
document.write(s1+" "+s2);
alert(s1+" "+s2);
}
</script>
</head>
<body>
Enter Your First Name :<input type="text" name="txt"> <br>
Enter Your Last Name :<input type="text" name="txt"> <br>
<input type="button" value="Click Here" onclick="test();">
</body>
</html>

4.2Creatingobject
InJavaScript,almost"everything"isanobject.
•Booleanscanbeobjects(ifdefinedwith
thenewkeyword)
•Numberscanbeobjects(ifdefinedwith
thenewkeyword)
•Stringscanbeobjects(ifdefinedwith
thenewkeyword)
•Datesarealwaysobjects
•Mathsarealwaysobjects
•Regularexpressionsarealwaysobjects
•Arraysarealwaysobjects
•Functionsarealwaysobjects
•Objectsarealwaysobjects

4.2Creatingobject
WithJavaScript,youcandefineandcreate
yourownobjects.
Therearedifferentwaystocreatenew
objects:
1.Createasingleobject,usinganobject
literal.
2.Createasingleobject,withthekeyword
new.
3.Defineanobjectconstructor,andthen
createobjectsoftheconstructedtype.

Byobjectliteral
□Theobjectliteralisaneasywayof
creating an object.
□Usinganobjectliteral,youbothdefineand
createanobjectinonestatement.
□Anobjectliteralisalistofname:valuepairs
(likeage:50)insidecurlybraces{}.
□Syntax:
var object_name={ property1:value1,
property2:value2,….
propertyN:valueN
};

Example
<html>
<head>
<script>
var student={name:"ram",location:"Ayodhya",
Age:10000};
document.write(student.name);
</script>
</head>
</html>

BycreatinginstanceofObject
□Thesecondwaytocreateanobjectis
usingnewkeywordwithobject().
□Thenewoperatorisusedtocreatean
instanceofanobject.
□Dot.andsquarebrackets[]canbeusedto
specifypropertiesandmethods.

□Syntax:
■var object_name= new Object();
■Object_name.property_name=property_value;
//specify property using dot
■Object_name[“property_name”]=property_value;
// specify property using []

Example
<html>
<head>
<script>
var student = new Object();
student.name="Ram";
student["location"]="Ayodhya";
document.write(student.name+"<br>");
document.write(student["location"]);
</script>
</head>
</html>

Byusinganobjectconstructor
□Firstcreateafunctionwithparameters
thenassignvaluetoeachparameters
usingthiskeyword.
□thiskeywordreferstothecurrentobject.

□Syntax:
function function_name(parameter1,
parameter2,...., parametern)
{
this.parameter1=value1;
this.parameter2=value2;
...
this.parametern=valuen;
}
object_name= new function_name(value1,
value2,....valuen);

Example:
<html>
<head>
<script>
functionstud(name,location,age)
{
this.name=name;this.location=location;this.age=age;
}
student = new stud("Ram","Ayodhya",1000);
document.write(student.name+"<br>");
document.writeln(student.location+"<br>");
document.writeln(student.age);
</script>
</head>
</html>

4.3Dateobject:
4.3.1 Date constructor:
•Date(),
•Date(milliseconds),
•Date(dateString),
•Date(year, month, day, hours,
minutes, seconds, milliseconds)

4.3.2 Date Methods:
•getDate(),
•getDay(),
•getMonth(),
•getHours(),
•setDate(),
•setMonth(),
•setDay(),
•toString()

Dateconstructor:
□Thepurposeofdateobjectistoworkwith
date(days,months,years)andtime
(milliseconds,seconds,minutes,hours).
□Dateobjectiscreatedusing newDate().
□Syntax:
vardateObj=newDate();

Date()
□Thisisthedefaultconstructorwithno
argument.
□ItinitializestheDateobjectwiththe
currentdateandtime.

Example:
<html>
<head>
<script>
var dateObj= new Date();
document.write(dateObj);
</script>
</head>
</html>

Date(milliseconds)
□Thismethodiswithintegerparameter.
□Itrepresentsthedateinmilliseconds.
□Syntax:
Date.getMilliseconds()

Example:
<html>
<head>
<script>
var dateObj= new Date();
document.write(dateObj.getMilliseconds());
</script>
</head>
</html>

Date(dateString)
□AdateStringparameterisstringvalue
representingadate,intheformataccepted
bytheDate.parse()method.

Example:
<html>
<head>
<script>
var str="8/25/2021";
dateObj= new Date(str);
document.write(dateObj);
</script>
</head>
</html>

Date(year, month, day, [ hours,
minutes, seconds, milliseconds])

Example:
<html>
<head>
<script>
bdate= new Date(1988,9,25,8,57,58,0);
document.write(bdate);
</script>
</head>
</html>

4.2.2DateMethods:
□getDate()
□getDay()
□getMonth()
□getHours()
□setDate
□setMonth()
□setDay()
□toString()

getDate()
□Itreturnstheday(from1to31)ofthe
month.

getDay()
□Itreturnsthedayoftheweek.
□Valueform0to6(SundaytoSaturday).

getMonth()
□It returns the month.
□The value is from 0to 11(January to December).

getHours()
□Itreturnsthehours.
□Valueisfrom0to23.

Example
<html>
<head>
<script>
var bdate= new Date();
document.write("Day of Month :"+bdate.getDate()+"<br>");
document.write("Day :"+bdate.getDay()+"<br>");
document.write("Month :"+bdate.getMonth()+"<br>");
document.write("Hour :"+bdate.getHours()+"<br>");
</script>
</head>
</html>

setDate()
□Itsetsthedayofgivendateobject.

setMonth()
□Itsetsthedayofthemonthofgivendate
object.

setDay()
□Itsetsthedayofthemonthofgivendate
object.

toString()
□Itconvertsdateobjecttostringvalue.

Example
<html>
<head>
<script>
var bdate= new Date();
bdate.setDate(10);
bdate.setMonth(10);
bdate.toString();
document.write("Day of Month :"+bdate.getDate()+"<br>");
document.write("Day :"+bdate.getDay()+"<br>");
document.write("Month :"+bdate.getMonth()+"<br>");
document.write("To String :"+bdate.toString()+"<br>");
</script>
</head>
</html>
Tags