javascript object and xml language

17
Methods of the String Object charAt - returns a character at a specified index in a string Javascript CODE EXAMPLE var str = "- 1); // "t"Web Development"; var char1 = str.charAt(0); // "W" var char2 = str.charAt(1); // "e" var lastChar = str.charAt(str.length one or more strings Javascript CODE EXAMPLE var str1 = "Programming"; charCodeAt - returns a character code at a specified index in a string Javascript CODE EXAMPLE var str = "Web Development"; var charCode1 = - 1); // "116" str.charCodeAt(0); // "87" var charCode2 = str.charCodeAt(1); // "101" var lastCharCode = str.charCodeAt(str.length concat - string concatenation to combine var str2 = " is fun"; var str3 = " and refreshing."; var result = str1.concat(str2,str3);

Upload: vidhya-venkatesan

Post on 23-Jun-2015

152 views

Category:

Technology


1 download

TRANSCRIPT

Page 1: Javascript object and XML Language

Methods of the String Object• charAt - returns a character at a specified index in a stringJavascript CODE EXAMPLEvar str = "- 1); // "t"Web Development";var char1 = str.charAt(0); // "W"var char2 = str.charAt(1); // "e"var lastChar = str.charAt(str.length  • one or more stringsJavascript CODE EXAMPLEvar str1 = "Programming";

charCodeAt - returns a character code at a specified index in a stringJavascript CODE EXAMPLEvar str = "Web Development";var charCode1 = - 1); // "116" str.charCodeAt(0); // "87"var charCode2 = str.charCodeAt(1); // "101"var lastCharCode = str.charCodeAt(str.length • concat - string concatenation to combine var str2 = " is fun";var str3 = " and refreshing.";var result = str1.concat(str2,str3);

 

Page 2: Javascript object and XML Language

indexOf - returns the index position where a substring is first found in a stringJavascript CODE EXAMPLEvar str = "Adam is nerdy and Susan is not.";var pos = str.indexOf("is"); // 5

 LastIndexOf - returns the position where a substring is last found in a stringJavascript CODE EXAMPLEvar str = "Adam is nerdy and Susan is not.";var pos = str.lastIndexOf("is"); // 24 • match - return an array containing all matches of a regular expression based matchJavascript CODE EXAMPLEvar str = "Hi weenis, you must be a weenis from weenis land.";var regExp = /weenis/gi;var result = str.match(regExp);TIP: "gi" in the regular expression syntax is to specify that I want a "global" and “case-insensitive" match to occur. You can omit those or just use one or the other to make the match run a tad bit differently.

Page 3: Javascript object and XML Language

 replace - replace string data using a regular expressionJavascript CODE EXAMPLEvar str = "Hi weenis, you must be a weenis from weenis land.";var regExp = /weenis/gi;var result = str.replace(regExp, “teenis");TIP: "gi" specifies that I want a "global" and "incase-sensitive" replace to occur. • search - search string for a match using a regular expressionJavascript CODE EXAMPLEvar str = "You look like poop today.";var regExp = /poop/;if(str.search(regExp) > -1) {    document.write("poop was found");} else {    document.write("no poop found");}TIP: search will return "-1" if no match is found, and returns the index position number of the first match if a match is found.

Page 4: Javascript object and XML Language

• split - split a string into an array of substrings by specifying a delimiter to split byJavascript CODE EXAMPLEvar str = "I love pie. I love cake.";var array1 = str.split("",11); //     I, ,l,o,v,e, ,p,i,e,.var array2 = str.split(" ");     //      I,love,pie.,I,love,cake.var array3 = str.split("love");  //   I , pie. I , cake.TIP: split can be given an optional second parameter for limiting how many items get placed into the resulting array. I used the optional limiter when creating the array1 variable above. • slice - return a portion of a string by targeting the index positions of charactersJavascript CODE EXAMPLEvar str = "My dog is cute.";var slice1 = str.slice(0,2); // "My"var slice2 = str.slice(str.length - 5,str.length - 1); // "cute" 

Page 5: Javascript object and XML Language

 substr - returns a substring of a specified length starting from a specified index positionJavascript CODE EXAMPLEvar str = "Welcome to the show!";var sbstr1 = str.substr(0,7); // Welcomevar sbstr2 = str.substr(2,11); // lcome to thvar sbstr3 = str.substr(5); // me to the show!TIP: Giving substr only one parameter will specify that you want the selection to go through to the end of the string. • substring - returns a substring from one index, up to another index but not including itJavascript CODE EXAMPLEvar str = "Big Wheels";var substring1 = str.substring(0,5); // Big Wvar substring2 = str.substring(5,str.length - 1); // heelvar substring3 = str.substring(2); // g WheelsTIP: Giving substring only one parameter will specify that you want the selection to go through to the end of the string.

Page 6: Javascript object and XML Language

toLowerCase - returns an all lowercase version of a stringJavascript CODE EXAMPLEvar str = "Here Lies Text.";var newStr = str.toLowerCase(); // here lies text. • toUpperCase - returns an all uppercase version of a stringJavascript CODE EXAMPLEvar str = "Here Lies Text.";var newStr = str.toUpperCase(); // HERE LIES TEXT. 

Page 7: Javascript object and XML Language

Properties of the String Object• length - returns the number of characters in a stringJavascript CODE EXAMPLEdocument.write("I like cake".length);

Javascript CODE EXAMPLE

var str = "I like cake";var strLen = str.length;document.write(strLen); 

Page 8: Javascript object and XML Language

Date Object Javascript

The Javascript Date object sports a large set of built-in methods for writing code that can get, set, calculate and render dates and times. Date objects should be created using the new operator in order to return actual date objects instead of strings.

Formats for establishing dates and times

Establish a date object representing the always current local date and time:Javascript CODE EXAMPLEvar today = new Date();document.write(today); // Thu Feb 06 2014 08:49:31 

Page 9: Javascript object and XML Language

Methods of the Date Object• getDate - returns local date of the month for a date object (1 - 31)Javascript CODE EXAMPLEvar myDate = new Date();var result = myDate.getDate(); // 6 • getDay - returns local day of the week as 0 - 6 (0 = sunday, 1 = monday, etc...)Javascript CODE EXAMPLEvar myDate = new Date();var result = myDate.getDay(); // 4 • getFullYear - returns local full year for a date objectJavascript CODE EXAMPLEvar myDate = new Date();var result = myDate.getFullYear(); • getHours - returns the local hours for a date object (0 - 23)Javascript CODE EXAMPLEvar myDate = new Date();var result = myDate.getHours(); 

Page 10: Javascript object and XML Language

  • getMinutes - returns local minutes for a date object (0 - 59)Javascript CODE EXAMPLEvar myDate = new Date();var result = myDate.getMinutes(); • getMonth - returns local month for a date object (0 - 11)Javascript CODE EXAMPLEvar myDate = new Date();var result = myDate.getMonth(); • getSeconds - returns local seconds for a date object (0 - 59)Javascript CODE EXAMPLEvar myDate = new Date();var result = myDate.getSeconds(); 

Page 11: Javascript object and XML Language

 setHours - sets local hours for a date object (0 - 23)Javascript CODE EXAMPLEvar myDate = new Date();myDate.setHours(17); • setMinutes - sets local minutes for a date object (0 - 59)Javascript CODE EXAMPLEvar myDate = new Date();myDate.setMinutes(40);  setMonth - sets local month for a date object (0 - 11)Javascript CODE EXAMPLEvar myDate = new Date();myDate.setMonth(0); // January setSeconds - sets local seconds for a date object (0 - 59)Javascript CODE EXAMPLEvar myDate = new Date();myDate.setSeconds(30);

Page 12: Javascript object and XML Language

XML

•  XML EXtensible Markup Language s a markup language which is like HTML.

• XML and HTML both use tags. But there are some differences between them: i 

• HTML was designed for how to display data. • XML was designed for how to store data.• HTML tags are predefined.• XML tags are not predefined. You must define your own tags.• XML can be used to simplify data storage and sharing. • With XML, data can be easily exchanged between computer and

database systems, even they are incompatible in any other ways. Because XML data is stored in text format, this makes it easier to export data from a system to an XML file, and then import it into another system

Page 13: Javascript object and XML Language

XML Format and StructureA XML document is consist of tags and data. All data in an XML document is wrapped by the tags. <?xml version="1.0" encoding="ISO-8859-1"?><customer>

<firstname>Michael</firstname><lastname>Smith</lastname><gender>male</gender>

<address>

<street>197 West Park Ave.</street><city>New York</city><state>NY</state><zip>11375</zip><country>US</country>

</address>

<phone>718-235-5670</phone><email>[email protected]</email>

</customer>

Page 14: Javascript object and XML Language

customer ->  firstname  lastname  gender  address ->  street

citystatezipcountry

  phone  email

TREE STRUCTURE OF xml ELEMENTS<root><child1>......</child1><child2><subchild1>.....</subchild1><subchild2>.....</subchild2></child2></root>

Page 15: Javascript object and XML Language

CONTAINER ELEMENTS

<bookstore><book><category>PHP Books</category> <link>http://www.itechcollege.com/books/PHP-n_150.html</link></book><book><category>Javascript Books</category> <link>http://www.itechcollege.com/books/Javascript-n_146.html</link></book></bookstore>

Page 16: Javascript object and XML Language

XML Attributes

In HTML we can have <table width="100">. The "width" attribute provides additional information about the <table> element. To provide additional information about elements, XML elements can also have attributes in the start tag, just like HTML. Please note, in an XML file, an attribute value must be quoted with either single or double quotes.

<customer gender="male"><firstname>John</firstname><lastname>Smith</lastname></customer>

<customer><gender>male</gender><firstname>John</firstname><lastname>Smith</lastname></customer>

In the first example gender is an attribute. In the last, gender is an element. Both examples contain the same information.

Page 17: Javascript object and XML Language

XML Syntax (1)

The following are the syntax rules:

1. A XML document must contain a pair, and only a pair, of root tags2. All XML elements must have a opening tag and a closing Tag.3. XML tags are case sensitive.4. An element in XML can has its child element.5. All elements in XML must be properly nested within each other.6. XML attribute values must be quoted.