JS Students'
JS Students'
• Scripting language
• E.g.
onClick, onMouseDown, onMouseUp, onFocus, onKeyPress, onLoad,
onUnload, onSubmit
JAVASCRIPT
• Accept input
• script tag
<html>
<head>
<script type="text/javascript">
//script here
</script>
</head>
</html>
• Old Browsers
<html>
<head>
<script type="text/javascript">
<!--
script here
-->
</script>
</head>
</html>
JAVASCRIPT & BROWSERS
• <noscript>
<html>
<head>
<script type="text/javascript">
<!--
script here
-->
</script>
</head>
<body>
<noscript>
<p>Browser could not render javascript
</p>
</noscript>
</body>
</html>
FIRST SCRIPT
• function function_name()
{
//code here
}
function_name()
• Declaration
var number;
var no1, no2, name;
• Initialization
var number = 10;
var institute=“sicsr”;
var marks = number + 10.2
• null
• Scope
• Concatenation operator : +
• 10+10
• 10+”10”
• 10+”10”+10
• 10+10+”10”
• 10+10+parseInt(”10”)
• 10+10+parseFloat (”10.2”)
• number to string : “”+10
WRITE SCRIPTS FOR THE FOLLOWING
• Accept two numbers from the user. Display four buttons on the
screen. The buttons should contain +, -, *, /. Display the result on
the browser, based on the button clicked.
(Hint: Create four functions, and call the corresponding function on
the click event of each button)
• Accept two strings from the user. Check whether both the strings
are equal and display appropriate message using alert.
• Accept two numbers from the user. Using ternary operator, display
the greater of the two numbers on the browser.
• Accept a colour from the user. Accept a text from the user.
Display the text in the colour specified by the user.
CONDITIONAL STATEMENTS
• SYNTAX :
if(condition)
{
//statements to execute if condition is true
}
else
{
//statements to execute if condition is false
}
CONDITIONAL STATEMENTS
• SYNTAX :
if(condition 1)
{
//statements to execute if condition1 is true
}
else if(condition 2)
{
//statements to execute if condition2 is true
}
else if(condition n)
{
//statements to execute if condition n is true
}
else
{
//statements to execute if none of the conditions are true
}
CONDITIONAL STATEMENTS
• Passing arguments
• Returning values
FUNCTIONS
• Accept a shape from the user. User can enter three shapes :
Circle, Rectangle, Square.
Also ask the user whether he wants to calculate the area or
the perimeter. User can enter “area”, or “perimeter”.
Pass both the user-entered values to a user defined function.
• while(condition)
{
//statements to be executed
}
• do
{
//statements to be executed
}while(condition);
• for(initialization; condition; increment/decrement)
{
//statements to be executed
}
LOOPS
for(i=0; ; i++) { }
for(i=0; i<10; ) { }
for( ; ; ) { }
for(i=0,j=10; i<10 && j>5; i++,j--)
{ //statements to be executed }
LOOPS
• Display:
* ***** 1 1
** **** 2 2 1 2
*** *** 3 3 3 1 2 3
**** ** 4 4 4 4 1 2 3 4
***** * 5 5 5 5 5 1 2 3 4 5
• Display all the leap years between 1900 and 2020 in the form of a
table with 4 columns. (Divisible by 400 or (divisible by 4 and not
divisible by 100))
• Display:
2
24
246
2468
2 4 6 8 10
LOOPS
• Accept 2 nos. from the user. Display the result of the first
number raised to the second number. Use any loop.
• Number of digits
• Reverse
RECURSIVE FUNCTIONS
• function x()
{
document.write(“inside function”);
x();
}
• What is an array?
• Creating an array
• sort()
• reverse()
• join([separator]) – default : ,
• pop()
• concat(array1, array2)
• Multi-dimensional Arrays
var x=[1,2,3,4,5,6];
var y=[x,”bba(it)”,”bca”];
document.write(y[0][1]]);
PROGRAMS
• Write a script to accept numbers from the user till the user
enters a negative number. When the user enters a negative
number, display the average of all the numbers entered,
excluding the negative number.
• Write a script to accept numbers from the user till the user
enters an odd number. When the user enters an odd number,
display the number of dozens that each number
represents(Do not include numbers not divisible by 12).
Math.min()
Math.max()
Math.round()
Math.ceil()
Math.floor()
Math.floor(Math.random() * 5)
Math functions
Math.abs()
Math.sqrt()
Math.pow(base, exponent)
Math.sin()
Math.cos()
Math.tan()
Math.PI
Date functions
Ba
ck
navigator screen document history
an location
(read-only)
Ve (read-only) d
rsi For
on wa
, form/object rd
Na Co bu
me nte tto
, nt ns
Plu object Are
g- a
ins (property, method, value)
DOM
• Referencing an object
- window.document.getElementById(“elementID”);
- window.document.getElementsByName(“elementName”)[i];
- document.getElementsByName(“nameInput”)[i].value
- document.getElementById(“nameId”).name
- document.getElementById(“id”).property=“value”
document.getElementById(“para”).innerHTML=“Paragraph”
DOM
Load Images
Image
Image 1
Name Image
Image 2
Name Image
Image 3
Name
form object
1. document.getElementById(‘formId’)
2. document.getElementsByTagName(‘form’)[0]
3. document.forms[0]
OR
document.forms[‘formName’]
OR
document.formName
form object
E.g.
<div id=“header”>
<form>
<input type=“text”>
</form>
<form>
<input type=“button”>
</form>
</div>
1. action
var url = formObject.action;
To Change :
formObject.action = “http://www.mysite.com/intro.html”
2. elements[]
1. submit()
2. reset()
Form Controls
2 ways of accessing:
- getElementById()
- document.formName.controlId
- document.formName.controlName
document.getElementById(‘uid’)
document.searchform.uid OR document.searchform.uname
document.searchform.elements[0]
document.forms[‘frm’ OR ‘searchform’].elements[‘uid’ OR
‘uname’]
document.forms[‘frm’ OR ‘searchform’].uid OR uname
Form Controls
select
Properties : disabled, length(no. of options), name, size(no. of
visible contents), selectedIndex, selectedOptions, value