Standard Classes
JavaScript provides a number of classes and methods for use by programmers. A few of those items are described below:
String Class
The String class methods must be called from a string variable. In other words, var str="programming", must first be declared and the methods would be called from the str variable (str.toUpperCase()). Many string method operate on indicies, a numbered order of characters. The important thing to note is that strings start counting at 0. So, in str, the index of the character p is 0.
- length - a property (not method) that returns the number of characters in the string (str.length returns 11)
- indexOf(string) - returns the starting index of the first occurence of string in str, -1 if not found (str.indexOf("gram") returns 3)
- lastIndexOf(string) - does the same as indexOf, but counts backwards from the end
- charAt(num) - returns the character at the specified index (str.charAt(4) returns "r")
- toUpperCase() - returns the string as all upper case (str.toUpperCase() returns PROGRAMMING)
- toLowerCase() - returns the string as all lower case
- substring(start, [end]) - returns a substring of the string starting at start, up to but not including end. If end is not specified, the remainder of the string is included (str.substring(3, 7) returns "gram", str.substring(7) returns "ming")
- link(url) - takes a string and makes it an active link to the url
Date object
The Date class methods allow access to information about the date and time. They are accessed using Date.functionName. The Date object relies on internal clock, if set wrong, the wrong information will show.
- getDate() – returns the day of the month
- getDay() – returns day of the week
- getFullYear() – returns full year
- getHours() – returns hour
- getMinutes() – returns minutes
- getMonth() – returns month as a number
- getSeconds() – returns seconds
Window Object
The window class controls the window that the content (document) appears in. We can open new windows and close others from within a document.
- close( ) – closes specified window: myWindow.close()
- open ( ) – opens a new window: myWindow=window.open(“source.html”,”target name”,”options”)
options: toolbar, location, directories, status, menubar, scrollbars, resizable (all yes|no),width, height(in pixels)
options are separated by commas, default is no
Document object
The document obect controls the content on the page. There are many properties that we can access, but the most common are:
- document.lastModified - displays the date/time of modification, automatically updated
- document.title – sets the document title
Location object
The location object relates to current URL and page. WE can use the href property to either access OR set the page location.
- location.href=”url” – sets address to url
History object
The History object contains information and methods relating to the page history. These controls can be useful for creating a book type setting or slide show.
- forward() – moves forward one entry
- back() – moves back one entry
Navigator object
The Navigator object returns information about the browser being used by the client. The methods and properties are called using navigator.function or property.
- javaEnabled() – returns true or false
- appName – return name of application
- appVersion – returns application version
- cookieEnabled – true or false to see if browser accepts persistent cookies
- platform – a string representing the platform that the browser is running on
- userAgent –a string representing the userAgent heade