3 - js

11

Upload: sheeba-dhuruvaraj

Post on 14-Apr-2018

217 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 3 - js

7/27/2019 3 - js

http://slidepdf.com/reader/full/3-js 1/11

Page 2: 3 - js

7/27/2019 3 - js

http://slidepdf.com/reader/full/3-js 2/11

Object Oriented Programming

JavaScript is an Object Oriented

Programming (OOP) language.

 An OOP language allows you to defineyour own objects and make your own

variable types.

 An object is just a special kind of data. An object has ‘properties’ and ‘methods’. 

Page 3: 3 - js

7/27/2019 3 - js

http://slidepdf.com/reader/full/3-js 3/11

Properties & Methods

Properties are the values associated withan object.

<script type="text/javascript">

var txt="Hello World!";

document.write(txt.length);</script>

Methods are the actions that can beperformed on objects.

<script type="text/javascript">

var str="Hello world!";

document.write(str.toUpperCase());

</script>

Page 4: 3 - js

7/27/2019 3 - js

http://slidepdf.com/reader/full/3-js 4/11

JS Array

Syntax:var myCars=new Array();

<html>

<body>

<script type="text/javascript">

var x;var mycars = new Array();

mycars[0] = "Saab";

mycars[1] = "Volvo";

mycars[2] = "BMW";

for (x in mycars)

{document.write(mycars[x] + "<br />");

}

</script>

</body>

</html>

Page 5: 3 - js

7/27/2019 3 - js

http://slidepdf.com/reader/full/3-js 5/11

Array Object Methods

Method Description concat() Joins two or more arrays and returns the result

 join() Puts all the elements of an array into a string. The elementsare separated by a specified delimiter 

pop() Removes and returns the last element of an array

push() Adds one or more elements to the end of an array and returnsthe new length

reverse() Reverses the order of the elements in an array

shift() Removes and returns the first element of an array

slice() Returns selected elements from an existing array

sort() Sorts the elements of an array

splice() Removes and adds new elements to an arraytoSource() Represents the source code of an object 1

toString() Converts an array to a string and returns the result

unshift() Adds one or more elements to the beginning of an array andreturns the new length

Page 6: 3 - js

7/27/2019 3 - js

http://slidepdf.com/reader/full/3-js 6/11

String Object MethodsMethod Description

anchor() Creates an HTML anchor 

big() Displays a string in a big font

blink() Displays a blinking string

bold() Displays a string in bold

charAt() Returns the character at a specified positioncharCodeAt() Returns the Unicode of the character at a specified

position

concat() Joins two or more strings

fixed() Displays a string as teletype text

fontcolor() Displays a string in a specified color fontsize() Displays a string in a specified size

fromCharCode() Takes the specified Unicode values and returns astring

indexOf() Returns the position of the first occurrence of aspecified string value in a string

italics() Displays a string in italic

Page 7: 3 - js

7/27/2019 3 - js

http://slidepdf.com/reader/full/3-js 7/11

 

Method Description

lastIndexOf() Returns the position of the last occurrence of aspecified string value, searching backwards from thespecified position in a string

link() Displays a string as a hyperlink

match() Searches for a specified value in a stringreplace() Replaces some characters with some other charactersin a string

search() Searches a string for a specified value

slice() Extracts a part of a string and returns the extracted partin a new string

small() Displays a string in a small fontsplit() Splits a string into an array of strings

strike() Displays a string with a strikethrough

sub() Displays a string as subscript

Page 8: 3 - js

7/27/2019 3 - js

http://slidepdf.com/reader/full/3-js 8/11

Method Description

substr() Extracts a specified number of characters in a string,

from a start index

substring() Extracts the characters in a string between two

specified indices

sup() Displays a string as superscripttoLowerCase() Displays a string in lowercase letters

toUpperCase() Displays a string in uppercase letters

toSource() Represents the source code of an object

Page 9: 3 - js

7/27/2019 3 - js

http://slidepdf.com/reader/full/3-js 9/11

JS Form Validation

JavaScript can be used to validate data inHTML forms before sending off the

content to a server.

Form data that typically are checked by aJavaScript could be:

has the user left required fields empty?

has the user entered a valid e-mail address?

has the user entered a valid date?

has the user entered text in a numeric field?

Page 10: 3 - js

7/27/2019 3 - js

http://slidepdf.com/reader/full/3-js 10/11

<html>

<head>

<script type="text/javascript">

function validate_required(field,alerttxt) {

with (field) {if (value==null||value=="") {

alert(alerttxt);return false;

}

else {

return true;

}

}}

function validate_form(thisform) {

with (thisform) {

if (validate_required(email,"Email must be filled out!")==false)

{email.focus();return false;}

}}

</script></head>

<body>

<form action="submit.htm" onsubmit="return validate_form(this)" method="post">

Email: <input type="text" name="email" size="30">

<input type="submit" value="Submit">

</form></body></html>

Page 11: 3 - js

7/27/2019 3 - js

http://slidepdf.com/reader/full/3-js 11/11

To check for valid email:

function validate_email(field,alerttxt)

{

with (field)

{

apos=value.indexOf("@");

dotpos=value.lastIndexOf(".");

if (apos<1||dotpos-apos<2){alert(alerttxt);return false;}

else {return true;}

}

}