CSS QB CT1 STARTING 10
CSS QB CT1 STARTING 10
<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 proper es of an object are used to define the
characteris cs of the object
You access the proper es of an object with a simple dot
nota on.
For example, A form object in a web page can have
proper es 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 func on associated with an
object to perform a specific task. Methods are defined
the way normal func ons are defined, except that they
have to be assigned as the property of an object.
For example, A submit bu on placed on a form is an
object. Clicking on submit bu on causes the bu on to
process a method i.e. when a click event occurs an
ac on is performed and method executes.
<html> <body>
<h2>JavaScript Objects</h2>
<p>An object method is a func on defini on, stored as a
property value.</p>
<script>
// Create an object: method.html
var person = {
firstName: "John",
lastName : "Doe",
id : 5566,
fullName : func on() {
return this.firstName + " " + this.lastName +" has id
"+this.id;
} };
// Display data from the object:
document.write( person.fullName());
</script> </body> </html>
3)List various assignment operators supported by
JavaScript, explain any two with the help of suitable
examples.(CO1/R)
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 exis ng 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 a er 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 isO en used to handle cleanup tasks or confirm user
ac ons before leaving the page.
c) onclick:
It executes when a mouse bu on is clicked over an element
Usage Commonly used to handle user interac ons such as
bu on clicks.
d) ondblclick:
executes when the mouse bu on is double clicked on the
element
Usage: Used to handle ac ons that should happen on a
double-click, such as edi ng text or opening addi onal
informa on.
<html>
<head>
< tle>Display Array Elements</ tle>
</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("A er 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 bu ons.
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>