CSS CT1 all
CSS CT1 all
<html>
<body>
<script>
var array =[5,1,9,7,5];
// sortng the array
sorted = array.sort();
document.write(sorted);
</script>
</body>
</html>
2) State the use of Object, Method and Property in
JavaScript.(CO1/R)
Object:
Example of object:
var person = {
firstName: "John",
lastName: "Doe",
age: 50,
eyeColor: "blue"
};
Property:
A property is a value that is associated with an object.
The properties of an object are used to define the
characteristics of the object
You access the properties of an object with a simple dot
notation.
For example, A form object in a web page can have
properties like width, height, etc.
Example:-
<html>
<body>
<h2>JavaScript Objects and Property usage</h2>
<script>
var person = { };
person['firstname'] = 'Maanav';
person['lastname'] = 'Desai';
document.write(person.firstname + " "+person.lastname
+"<P>");
person = {'firstname': 'John', 'lastname': 'Patel'}
document.write(person['firstname'] +" "+
person['lastname']);
</script>
</body>
</html>
Method:
A method is used to define a function associated with an
object to perform a specific task. Methods are defined
the way normal functions are defined, except that they
have to be assigned as the property of an object.
For example, A submit button placed on a form is an
object. Clicking on submit button causes the button to
process a method i.e. when a click event occurs an
action is performed and method executes.
<html> <body>
<h2>JavaScript Objects</h2>
<p>An object method is a function definition, stored as a
property value.</p>
<script>
// Create an object: method.html
var person = {
firstName: "John",
lastName : "Doe",
id : 5566,
fullName : function() {
return this.firstName + " " + this.lastName +" has id
"+this.id;
} };
// Display data from the object:
document.write( person.fullName());
</script> </body> </html>
Example:
<script>
// = is used to assign a value to a variable
var num = 10;
document.write(num); // num 10
/* += first add the value to the existing value of the
variable then assign it the new added
value */
num +=10;
example:
<!DOCTYPE html>
<html>
<body>
<script>
var car = {
brand: "Toyota",
color: "Blue",
get getBrand () {
return this.brand;
},
get getColor () {
return this.color;},
set setBrand (newBrand)
{
this.brand = newBrand;
},
set setColor (newColor) {
this.color = newColor; }
};
document.write("Car Brand: " + car.brand + "Car Color: " +
car.color);
car.setBrand = "Tesla";
car.setColor = "Red";
document.write("Car Brand: " + car.brand + "Car Color: " +
car.color);
</script>
</body>
</html>
a) onload:
It executes when the browser finishes loading a window or all
frames within a frameset
Usage It is Commonly used to execute code after a web page
or element has fully loaded.
b) onunload:
It executes when the browser removes a document from a
window or a frame
Usage It isOften used to handle cleanup tasks or confirm user
actions before leaving the page.
c) onclick:
It executes when a mouse button is clicked over an element
Usage Commonly used to handle user interactions such as
button clicks.
d) ondblclick:
executes when the mouse button is double clicked on the
element
Usage: Used to handle actions that should happen on a
double-click, such as editing text or opening additional
information.
<html>
<head>
<title>Display Array Elements</title>
</head>
<body>
<script>
var fruits = new Array();
fruits[0] = ‘mango ';
fruits[1] = 'apple';
fruits [2] = 'kiwi';
fruits[3] = 'cherry';
fruits[4] = 'banana';
alert(fruits);
</script>
</body>
</html>
8) State the features of Javascript.(CO2/R)
<html>
<body>
<script>
var person = {
firstname:"John",
lastname:"Doe",
age:50,
eyecolor:"blue"
};
delete person.eyecolor; //delete person eyecolor
document.write("After delete "+ person.firstname +" "+
person.lastname +" "
+person.age +" "+ person.eyecolor);
</script>
</body>
</html>
prompt() :
The prompt () method displays a dialog box that
prompts the visitor for input.
The prompt () method returns the input value if the
user clicks "OK".
If the user clicks "cancel" the method returns null.
Syntax: window.prompt (text, defaultText)
Example:
<html>
<script type="text/javascript">
funcƟon msg(){
var v= prompt("Who are you?");
alert("I am "+v);
}
</script>
<input type="buƩon" value="click" onclick="msg()"/>
</html>
confirm()
It displays the confirm dialog box. It has message with
ok and cancel buttons.
Returns Boolean indicaƟng which buƩon was pressed
Syntax: window.confirm("sometext");
Example :
<html>
<script type="text/javascript">
funcƟon msg(){
var v= confirm("Are u sure?");
if(v==true){
alert("ok");
}
else{
alert("cancel");
}
}
</script>
<input type="buƩon" value="delete record"
onclick="msg()"/>
</html>