CSS Chapter 1 Overall
CSS Chapter 1 Overall
He created the JavaScript programming language and co-founded the Mozilla project, the Mozilla Foundation, and the Mozilla
Corporation.
JavaScript is a programming language for use in HTML pages.
⮚ It was invented in 1995 at Netscape Corporation.
⮚ JavaScript programs are run by an interpreter built into the user's web browser.
⮚ JavaScript can dynamically modify an HTML page.
⮚ A program written in JavaScript can access the elements of a web page, and the browser window in which it is running, and perform
actions on those elements, as well as create new page elements.
1. Features of JavaScript
⮚JavaScript can dynamically modify an HTML page
⮚JavaScript can react to user input
⮚JavaScript can validate user input
⮚JavaScript can be used to create cookies
⮚JavaScript is a full-featured programming language
⮚JavaScript user interaction does not require any communication with the server
⮚ It is supported by almost all browsers
⮚ It supports dynamic typing i.e same variable can be used to hold different values at different times.
⮚ It supports object oriented paradigm and functions.
⮚Supports use of regular expression using which text-pattern matching can be done.
What Can U Do With JavaScript?
⮚Opening new windows with a specified size, position, and style (for example, whether the window has borders, menus, toolbars, and so
on)
⮚Providing user-friendly navigation aids such as drop-down menus
⮚Validation of data entered into a web form to make sure that it is of an acceptable format before the form is submitted to the web server
⮚Changing how page elements look and behave when particular events occur, such as the mouse cursor moving over them
⮚Detecting and exploiting advanced features supported by the particular browser being used, such as third-party plug-ins, or native
support for new technologies
How To Use JavaScript
i. You can include JavaScript statements directly into your HTML code by placing them between <script> and </script> tags
<script type="text/javascript">
... JavaScript statements ...
</script>
ii. You can write the JavaScript code in a separate file with .js extension and include this file in the html file using the src property of
the script tag. This is called as remote javascript
eg. javascriptfile.js
document.write("Hello from javascript File");
hello.html
<html>
<head><script src="javascriptfile.js"></head>
<body>
</body>
</html>
2. Object name, property, method, dot syntax, main event
DOM(document object model)
⮚Each time your browser is asked to load and display a page, it needs to interpret (we usually use the word “parse”) the source
code contained in the HTML file comprising the page.
⮚As part of this parsing process, the browser creates a type of internal model known as a DOM representation based on the
content of the loaded document.
⮚ It is this model that the browser then refers to when rendering the visible page.
⮚You can use JavaScript to access and edit the various parts of the DOM representation, at the same time changing the way the
user sees and interacts with the page in view.
⮚document object contains all of the HTML and other resources that go into making up the displayed page.
⮚ Location object-contains details of the URL of the c urrently loaded page
⮚History object-contains details of the browser‟s previously visited pages
⮚navigator object - stores details of the browser type, version, and capabilities
Object Properties:
1.Object Name: Object is entity. In JavaScript document,window, forms, fields, buttons are some properly used objects.Each object
is identified by ID or name. Array of objects or Array of collections can also becreated.
2. Property: It is the value associated with each object. Each object has its own set ofproperties.
3. Method: It is a function or process associated with each object. Ex: for document object write is a method.
Object Notation: The notation we use to represent objects within the tree uses the dot or period: parent.child
Eg : window.location
This notation can be extended as many times as necessary to represent any object in the tree.
For example : object1.object2.object3 represents object3, whose parent is object2, which is itself a child of object1
The <body> section of your HTML page is represented in the DOM as a child element of the document object; we would access it like
this:
window.document.body
Instead of object the last part can also be a method or property
Eg : window.document.title (here title is a property)
Dot Syntax: It is used to access properties and methods of object.
Main Event: Event causes JavaScript to execute the code. In JavaScript when user submits form, clicks button, writes something to text
box the corresponding event gets triggered and execution of appropriate code is done through Event Handling.
* Multiplication c=a*b
/ Division c=a/b
% Mod c=a%b
|| OR Operator 0||1
3. Increment ++ Increment by one ++i or i++
== Equal to a==4
statement.
delete Delete Operator deletes a property
from the object.
Expressions
An expression is any valid unit of code that resolves to a value.
JavaScript has the following expression categories:
⮚Arithmetic: evaluates to a number, for example 3.14159.
⮚String: evaluates to a character
⮚ Logical: evaluates to true or false.
⮚Primary expressions: Basic keywords and general expressions in JavaScript.
⮚ Left-hand-side expressions: Left values are the destination of an assignment.
Primary Expressions
1) this
- Use the this keyword to refer to the current object. In general, this refers to the calling object in a method.
- It is used either with the dot or the bracket notation:
this['propertyName'] or this.propertyName
2) Grouping Operator
- The grouping operator ( ) controls the precedence of evaluation in expressions. For example, you can override multiplication and
division first, then addition and subtraction to evaluate addition first.
var a = 1;
var b = 2;
var c = 3;
a + b * c // 7
a + (b * c) // 7
(a + b) * c // 9
3) Left-hand-side expressions
- Left values are the destination of an assignment.
4) New
- You can use the new operator to create an instance of a user-defined object type or of one of the built-in object types.
var objectName = new objectType([param1, param2, ..., paramN]);
5) super
The super keyword is used to call functions on an object's parent. It is useful with classes to call the parent constructor.
6)Spread operator
The spread operator allows an expression to be expanded in places where multiple arguments (for function calls) or multiple
elements (for array literals) are expected.
Eg :var parts = ['shoulders', 'knees'];
var lyrics = ['head', ...parts, 'and', 'toes'];
function f(x, y, z) { }
var args = [0, 1, 2];
f(...args);
5. Object and Array initializers, function Definition expression
- Object and array initializers are expressions whose value is a newly created object or array. These initializer expressions are sometimes
called “object literals” and “array literals.
- An array initializer is a comma-separated list of expressions contained within square brackets. The value of an array initializer is a newly
created array. The elements of this new array are initialized to the values of the comma-separated expressions: [ ] // An empty array: no
expressions inside brackets means no elements
[1+2,3+4] // A 2-element array. First element is 3, second is 7
- The element expressions in an array initializer can themselves be array initializers, which means that these expressions can create nested
arrays:
var matrix = [[1,2,3], [4,5,6], [7,8,9]];
- Undefined elements can be included in an array literal by simply omitting a value between commas. For example, the following array
contains five elements, including three undefined elements:
var sparseArray = [1,,,,5];
- Object initializer expressions are like array initializer expressions, but the square brackets are replaced by curly brackets, and
each subexpression is prefixed with a property name and a colon:
var p = { x:2.3, y:-1.2 }; // An object with 2 properties
var q = { }; // An empty object with no properties
q.x = 2.3;
q.y = -1.2; // Now q has the same properties as p
- Object literals can be nested.
For example:
var rectangle = { upperLeft: { x: 2, y: 2 }, lowerRight: { x: 4, y: 5 } }
<script>
var a=20;
if(a==10){
else if(a==15){
else if(a==20){
else{
</script>
switch(expression){
case value1:
code to be executed;
break;
case value2:
code to be executed;
break;
......
default:
code to be executed if above values are not matched;
}
Ex:
<script>
var grade='B';
var result;
switch(grade){
case 'A':
result="A Grade";
break;
case 'B':
result="B Grade";
break;
case 'C':
result="C Grade";
break;
default:
result="No Grade";
}
document.write(result);
</script>
v. for loop
Syntax:- for loop
for (initialization; condition; increment)
{
code to be executed
}
do{
code to be executed
}while (condition);
<script>
var i=21;
do{
document.write(i + "<br/>");
i++;
}while (i<=25);
</script>
viii. continue statement The continue statement breaks one iteration (in the loop), if a specified condition occurs, and continues
with the next iteration in the loop
var text = ""
var i;
for (i = 0; i < 5; i++) {
if (i === 3) {
continue;
}
text += "The number is " + i + "<br>";
}
ix. break statement: The break statement can also be used to jump out of a loop. The break statement breaks the loop and continues
executing the code after the loop (if any):
Syntax:
jump-statement;
break;
Ex:
for (i = 0; i < 10; i++)
{
if (i === 3)
{ break; }
text += "The number is " + i + "<br>";
}
8. Querying and setting properties
JavaScript Properties
⮚ Properties are the values associated with a JavaScript object.
⮚ A JavaScript object is a collection of unordered properties.
⮚ Properties can usually be changed, added, and deleted, but some are read only
Querying Properties
⮚ The syntax for accessing the property of an object is:
objectName.property // person.age
objectName["property"] // person["age"]
objectName[expression] // x = "age"; person[x]
Deleting Properties
⮚ The delete keyword deletes a property from an object
⮚ The delete keyword deletes both the value of the property and the property itself.
⮚ After deletion, the property cannot be used before it is added back again.
⮚ The delete operator is designed to be used on object properties. It has no effect on variables or functions.
⮚The delete operator should not be used on predefined JavaScript object properties. It can crash your application.
Ex: var person = {firstName:"John", lastName:"Doe", age:50, eyeColor:"blue"};
delete person.age; // or delete person["age"];
⮚Getters and setters in JavaScript are used for defining computed properties, or accessors. A computed property is one that uses a
function to get or set an object value.
Example of getter method :
var person = {
firstName: "John",
lastName : "Doe",
language : "en",
get lang() {
return this.language;
}
};
Example of setter method :
var person = {
firstName: "John",
lastName : "Doe",
language : "",
set lang(lang) {
this.language = lang;
}
};
// Set an object property using a setter:
person.lang
Q. Explain prompt() and confirm() method of Java script with syntax and example (W-19)
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">
function msg(){
var v= prompt("Who are you?");
alert("I am "+v);
}
</script>
<input type="button" value="click" onclick="msg()"/>
</html>
confirm()
It displays the confirm dialog box. It has message with ok and cancel buttons.
Returns Boolean indicating which button was pressed
Syntax:
window.confirm("sometext");
Example :
<html>
<script type="text/javascript">
function msg(){
var v= confirm("Are u sure?");
if(v==true){
alert("ok");
}
else{
alert("cancel");
}
}
</script>
<input type="button" value="delete record" onclick="msg()"/>
</html>