PHP Module 2-1
PHP Module 2-1
PHP Module 2-1
MODULE 2
MAJLIS ARTS AND SCIENCE COLLEGE, PURAMANNUR
Ans:Netscape
Ans: function
Ans: focus
Ans: <script>
JavaScript's interaction with HTML is handled through events that occur when the user or the browser
manipulates a page.
2 types
2. User events : Which occur when the user interacts with elements in the page using pointing device.
2 types:
Ans:
Eg:
var x;
x = 6;
8. What is a function? How can you define and call a function in java script?
Ans:
followed by a name,
Syntax:
// code to be executed
Eg:
{
return p1 * p2;
Example:
<HTML>
<HEAD>
<SCRIPT LANGUAGE="JavaScript">
function myFunction ()
</SCRIPT>
</HEAD>
<BODY>
an event occurs
</BODY>
</HTML>
Ans:
JavaScript comments can be used to explain JavaScript code, and to make it more readable.
Two types:
1. Single Line Comments
2. Multi-line Comments
Any text between // and the end of the line will not be executed.
Eg:
Multi-line comments
Eg:
/*this is
an example*/
1. Alert
An alert box is often used if you want to make sure information comes through to the user.
When an alert box pops up, the user will have to click "OK" to proceed.
Syntax
Eg:
alert(”Javascript!");
2. Confirm
A confirm box is often used if you want the user to verify or accept something.
When a confirm box pops up, the user will have to click either "OK" or "Cancel" to proceed.
If the user clicks "OK", the box returns true. If the user clicks "Cancel", the box returns false.
Syntax
confirm("sometext");
// window.confirm("sometext");
3. Prompt Box
A prompt box is often used if you want the user to input a value before entering a page.
When a prompt box pops up, the user will have to click either "OK" or "Cancel" to proceed after entering
an input value.
If the user clicks "OK" the box returns the input value. If the user clicks "Cancel" the box returns null.
Syntax
prompt("sometext","defaultText");
// window.prompt("sometext","defaultText");
break
syntax: break;
continue
syntax: continue;
Ans:
Conditional Statements
1. Simple if
2. if.…else
3. else if Statement
4. Switch….case
Simple if
Syntax:
if (condition)
{
// block of code to be executed if the condition is true
}
If…else
Syntax
if (condition)
else
Eg:
<html>
<head>
<script language=“javascript”>
function check()
var a=50;
if(a % 2==0)
document.write(“Even number”);
else
document.write(“odd number”);
</script></head>
<body>
</body></html>
else if
Syntax
if (condition1)
else if (condition2)
else
Switch…case
Syntax
switch(expression)
case value 1:
// code block
break;
case value2:
// code block
break;
……..
……..
case valuen:
// code block
break;
default:
// code block
13. How do we write java script with HTML programming? Give an example.
Ans: 3 ways
Eg:
<html>
<head>
<script language=“javascript”>
function myFunction()
{
document.write("Hello world");
}
</script>
</head>
<body>
<button type="button" onclick="myFunction()">Try it</button>
</body>
</html>
Eg:
<html>
<body>
<script language=“javascript”>
function myFunction()
document.write("hello world");
</script>
</body></html>
In an external file
External scripts are practical when the same code is used in many different web pages.
To use an external script, put the name of the script file in the src (source) attribute of a <script> tag.
External scripts can be referenced with a full URL or with a path relative to the current web page.
You can place an external script referencein <head> or <body> s you like.
Eg:
message.js
function msg()
document.write("Hello world");
<html>
<head>
</head>
<body>
<form>
</form>
</body>
</html>
Ans:
write() Method
Syntax:
document.write("<h1>Hello World!</h1>”);
document.write(a);
writeln() Method
used to write a document with additional property of newline character after each statement.
Syntax:
Eg:
document.writeln("<h1>Hello World!</h1>”);
document.writeln(a);
Ans:
1. Arithmetic Operators
2. Assignment Operators
3. String Operators
4. Comparison Operators
5. Logical Operators
6. Bitwise Operators
7. Conditional Operator
Arithmetic operators
Operator Description
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulus (Remainder)
** Exponentiation
++ Increment
-- Decrement
Eg: 1
var x = 5;
var z = ++x; // var z = x++;
document.write(z);
Eg: 2
var x = 5;
var z = x ** 2;
// x ** y produces the same result as Math.pow(x,y)
Assignment opeartor
Operator Example Same As
= x=y x=y
+= x += y x=x+y
-= x -= y x=x–y
*= x *= y x=x*y
/= x /= y x=x/y
%= x %= y x=x%y
**= x **= y x = x ** y
String opreators
The + operator can also be used to concatenate strings.
Example: 1
var txt1 = "John";
var txt2 = "Doe";
var txt3 = txt1 + txt2; // txt3=JohnDoe
Example: 2
var txt1 = "What a very ";
txt1 += "nice day"; // txt1=What a very nice day
Example: 3
var x = 5 + 5; // x=10
var y = "5" + 5; // y=55
var z = 5 + "5"; // z=55
Comparison operators
== equal to x == 8 false
x == 5 true
x == "5“ true
=== equal value and equal type x === 5 true
x === "5“ false
!= not equal x != 8 true
!== not equal value or not equal type x !== 5 false
x !== "5“ true
x !== 8 true
> greater than x>8 false
< less than x<8 true
>= greater than or equal to x >= 8 false
<= less than or equal to x <= 8 true
Logical operators
Bitwise operators
Operator Description Example Same as Result Decimal
Conditional operator
syntax:
variablename = (condition) ? value1:value2
Example:
varvoteable = (age < 18) ? "Too young":"Old enough";
In this type of loops the test condition is tested or evaluated at the end of loop body. Therefore,
the loop body will execute atleast once, irrespective of whether the test condition is true or false.
for loop
Syntax:
{ statement(s) }
Eg:
var i;
text += i;
document.write(text);
while loop
Syntax
while(condition)
statement(s)
Eg:
var text = ““;
var i=1;
while(i<=5)
text += i;
i++;
document.write(text);
do…while loop
Syntax
do
statement(s)
} while(condition);
Eg:
var i=1;
do
text += i;
i++;
}while(i<=5);
document.write(text);
Ans:
sort() :sorts an array alphabetically
Example
fruits.sort(); // Apple,Banana,Mango,Orange
Example
fruits.sort();
fruits.reverse(); // Orange,Mango,Banana,Apple
Example
varmyChildren = myGirls.concat(myBoys);
Example
[The slice() method creates a new array. It does not remove any elements from the source array.]
Example
alert(mystring);
String methods
Method purpose
string.fontcolor("color")
indexOf() : returns the index of (the position of) the first occurrence of a specified text in a string
Example
pos = mystring.indexOf(“to"); // 8
lastIndexOf() : Returns the position of the last found occurrence of a specified value in a string
Eg:
substr() : Extracts the characters from a string, beginning at a specified start position, and through the
specified number of character.
Syntax
string.substr(start, length)
Start : The position where to start the extraction. First character is at index 0.
length : Optional. The number of characters to extract. If omitted, it extracts the rest of the string
substring() : Extracts the characters from a string, between two specified indices
Syntax
string.substring(start, end)
Start : Required.
End : Optional. The position (up to, but not including) where to end the extraction. If omitted, it
extracts the rest of the string
Ans:
2 types
User events : Which occur when the user interacts with elements in the page using pointing device.
2 types:
Window events
onunload : once a page has unloaded (or the browser window has been closed).
onerror : when an error occurs while loading an external file (e.g. a document or an image).
Keyboard events
Example:
<html>
<body>
<script>
function myFunction()
</script>
</body>
</html>
Mouse events
Events that occur when the mouse interacts with the HTML document.
Onmousedown : The event occurs when the user presses a mouse button over an element.
Onmouseup : The event occurs when a user releases a mouse button over an element.
onmouseover : The event occurs when the pointer is moved onto an element.
Onmouseout : The event occurs when a user moves the mouse pointer out of an element.
Onmousemove : The event occurs when the pointer is moving while it is over an element.
Onmouseenter : The event occurs when the pointer is moved onto an element.
Eg:
<button onclick="myFunction()">