Vous êtes sur la page 1sur 11

Built in objects:: The document object: A document is a web page that is being either displayedbor created.

The document has number of properties that can be accessed by javascrpt programs and used to manipulate the content of the page. Some of these properties csn be used to create HTML pages from with in javascript while others used to change the operation of current page. The JavaScript Document object is the container for all HTML HEAD and BODY objects associated within the HTML tags of an HTML document.

Write - Add data to a document. Writes the values passed to the write function to the document. document.write("<H3>") document.writeln("This is a Header") document.write("</H3>")

writeln - Adds the passed values to the document appended with a new line character.

bgColor Property This property defines a document's background color. The "colorinfo" argument is a string that can contain either the hexadecimal definition of the color or it's literal description. Syntax: document.bgColor = "colorinfo" fgColor Property This property defines a document's foreground (text) color. The "colorinfo" argument is a string that can contain either the hexadecimal definition of the color or it's literal description. Syntax: document.fgColor = "colorinfo"

anchors Property anchors are created using <a name =..> these are commonly used for moving around inside a large page This property is an array containing references to all the named Anchor objects in the current document. Syntax: document.anchors["anchorID"] forms Property This property is an array containing references to all the Form objects in the current document. Syntax: document.forms["formID"] links Property This property is an array containing references to all the Area and Link objects in the current document. Syntax: document.links["linkID"] layers Property

This property is an array containing references to all the Layer objects in the current document. Syntax: document.layers["layerID"] close Method This method closes an output stream previously opened with the document.open method and forces data collected from any instances of the document.write or document.writeln methods to be displayed. Syntax: document.close( ) the window object: As the top level object in the JavaScript client hierarchy, every browser window and frame has a corresponding Window object, created automatically with every instance of a <BODY> or <FRAMESET> tag. open():
This opens a new window which contains the document specified by url.the window is given an identifying name so that it can be manipulated indiidually open() method opens a new browser window.

Syntax
Window.open(URL,name,specs,) Parameter URL Name Description Optional. Specifies the URL of the page to open. If no URL is specified, a new window with about:blank is opened Optional. Specifies the target attribute or the name of the window. The following values are supported:

Specs

Optional. A comma-separated list of items. The following values are supported: Directories=yes|no|1|0 Whether or not to add directory buttons. Default is yes. IE only fullscreen=yes|no|1|0 Whether or not to display the browser in fullscreen mode. Default is no. A window in full-screen mode must also be in theater mode. IE only Height=pixels left=pixels location=yes|no|1|0 menubar=yes|no|1|0 resizable=yes|no|1|0 The height of the window. Min. value is 100 The left position of the window Whether or not to display the address field. Default is yes Whether or not to display the menu bar. Default is yes Whether or not the window is resizable. Default is yes

scrollbars=yes|no|1|0 Whether or not to display scroll bars. Default is yes

status=yes|no|1|0 titlebar=yes|no|1|0

Whether or not to add a status bar. Default is yes Whether or not to display the title bar. Ignored unless the calling application is an HTML Application or a trusted dialog box. Default is yes Whether or not to display the browser toolbar. Default is yes The width of the window. Min. value is 100

toolbar=yes|no|1|0 width=pixels

close Method This method is used to close a specified window. If no window reference is supplied, the close() method will close the current active window. Note that this method will only close windows created using the open() method; if you attempt to close a window not created using open(), the user will be prompted to confirm this action with a dialog box before closing. The single exception to this is if the current active window has only one document in its session history. In this case the closing of the window will not require confirmation. Syntax: window.close( ) scrollTo Method This method scrolls the contents of a window, the specified co-ordinate becoming the top left corner of the viewable area. Syntax: window.scrollTo(xPosition, yPosition) widh=pixels height=pixels:when positioning contenet ,especially on dynamic pages ,it is useful to be able to locate it what the resolution of the screen or sixe of window being used these values are easily available.these properties can be used to set the window sixe. The following sode shows an example newWin=open(address,newWin,status=0,width-100,height=100,resizable=0);

Examples
Example 1
The following example opens www.w3schools.com in a new browser window: <html> <head> <script type="text/javascript"> function open_win() { window.open("http://www.w3schools.com") } </script> </head> <body> <input type=button value="Open Window" onclick="open_win()" /> </body> </html>
Try it yourself

Example 2
The following example opens an about:blank page in a new browser window: <html> <body> <script type="text/javascript"> myWindow=window.open('','','width=200,height=100') myWindow.document.write("<p>This is 'myWindow'</p>") myWindow.focus() </script> </body> </html>
Try it yourself

PROPERTIES This property contains details of the current URL of the window and its value is always the Location object for that window. Syntax: window.location

menubar Property This property relates to the area of a browser's window that contains the various pull-down menus (File, Edit, View, etc.). The menubar property has its own property, visible, that defaults to true (visible) and can be set to false (hidden). Syntax: window.menubar[.visible = false]

status Property This property, which can be set at any time, is used to define the transient message displayed in a window's status bar such as the text displayed when you onMouseOver a link or anchor. Syntax: window.status(= "message") statusbar Property This property relates to the browser's status bar. The statusbar property has its own property, visible, that defaults to true (visible) and can be set to false (hidden). Syntax: window.statusbar[.visible = false] toolbar Property This property sets or returns a Boolean value that defines whether the browser's tool bar is visible or not. The default is true (visible). False means hidden. It can only be set before the window is opened and you must have UniversalBrowserWrite privilege. Syntax: window.toolbar[.visible = false] window Property This property is a reference (or synonym) for the current window or frame.

Syntax: window.property or method METHODS close Method This method is used to close a specified window. If no window reference is supplied, the close() method will close the current active window. Note that this method will only close windows created using the open() method; if you attempt to close a window not created using open(), the user will be prompted to confirm this action with a dialog box before closing. Syntax: window.close( ) open Method This method is used to open a new browser window. Syntax: window.open(URL, name [, features]) scrollTo Method This method scrolls the contents of a window, the specified co-ordinate becoming the top left corner of the viewable area. the browser object: No two browser models will process javascript in the same way. It is important to find ut which browser is beinh used to view the page. The browser is a javascript object ans can be queried from with in your code. He browser object is actually called the navigator object The Navigator object is designed to contain information about the version of Netscape Navigator which is being used. However, it can also be used with Internet Explorer. All of its properties, which are read-only, contain information about different aspects of the browser. PROPERTIES appCodeName Property This property contains a string which specifies the code name of the browser.the internal name of browser. For both major products this is Mozilla. Syntax: navigator.appCodeName

appName Property This property is a string that specifies the name of the browser. With a Netscape browser this property contains the string "Netscape", while with an IE explorer it contains "Microsoft Internet Explorer". Syntax: navigator.appName appVersion Property This property contains information about the browser version being used. If it is a Netscape browser, it contains the release number, the language used, the platform on which the browser is running, Syntax: navigator.appVersion

mimeTypes Property This property is an array of all the MIME (Multipart Internet Mail Extension) types supported by the client. Syntax: navigator.mimeTypes plugins Property This property is an array of all the plugins installed on the client. The plugin property also has its own method: plugins.refresh Syntax: navigator.plugins userAgent Property This property contains a string representing the value of the user-agent header sent by the client to the server in the http protocol. This information consists of the code name and the version of the browser, and is used by the server to identify the client. Syntax: navigator.userAgent <html> <body> <script type="text/javascript"> document.write("Browser CodeName: " + navigator.appCodeName); document.write("<br /><br />"); document.write("Browser Name: " + navigator.appName); document.write("<br /><br />"); document.write("Browser Version: " + navigator.appVersion);

document.write("<br /><br />"); document.write("Cookies Enabled: " + navigator.mimeTypes); document.write("<br /><br />"); document.write("Platform: " + navigator.plugins); document.write("<br /><br />"); document.write("User-agent header: " + navigator.userAgent); </script> </body> </html> the date object: Date object: Javascript includes a well developed Date class which provides functions to perform many different date manipulations. In javascript dates and times represent the number of milliseconds aince 1st January 1970 UTC.like other languages ,javascript has two sepatare notations of time they are UTC and local.UTC stands for universal time also known as Greenwhic Mean Time,which is the standard time throughout the world.local tine is the time on the machne which is executing the script.a javascript Date object can represent dates from 01/01/1970.since this range of dates clearly covers several millennia ,it is important to specify the years fully.if u r using 2001,the use 2001 not 01. Some of the more useful functions for setting and reteieving values from Date objects.

Date() - Use the current date and time to create an instance of the object date. Date(dateString) - Use the date specified by the string to create the instance of the date object. String format is "month day, year hours:minutes:seconds". Date(year, month, day, hours, minutes, seconds) - Create an new date object based up on numerical values for the year, month and day.optional values can be given. january is represented by the integer value 0 and December by 11.. Parse(string): erturns the number of milliseconds since 01-011970 .the string format must being the following format

Mon,9 april 2001 14:02:35

Date functions: Once a Date object is created, a number of methods allow you to operate on it. Most methods allow you to get and set the year, month, day, hour, minute, second, and milliseconds of the object, using either local time or UTC (universal, or GMT) time. There are four ways of instantiating a date: new new new new Date() // current date and time Date(milliseconds) //milliseconds since 1970/01/01 Date(dateString) Date(year, month, day, hours, minutes, seconds, milliseconds)

Some examples of instantiating a date: today = new Date() d1 = new Date("October 13, 1975 11:13:00") d2 = new Date(79,5,24) d3 = new Date(79,5,24,11,33,0)

Example 1- write out current date To write out today's date for example, you would do: <script type="text/javascript"> var var var var mydate= new Date() theyear=mydate.getFullYear() themonth=mydate.getMonth()+1 thetoday=mydate.getDate()

document.write("Today's date is: ") document.write(theyear+"/"+themonth+"/"+thetoday) </script>

Set Dates We can easily manipulate the date by using the methods available for the Date object. In the example below we set a Date object to a specific date (14th January 2010): var myDate=new Date(); myDate.setFullYear(2010,0,14); Compare Two Dates The Date object is also used to compare two dates. The following example compares today's date with the 14th January 2010: var myDate=new Date(); myDate.setFullYear(2010,0,14); var today = new Date(); if (myDate>today) { alert("Today is before 14th January 2010"); } else { alert("Today is after 14th January 2010"); } getDate Method This method returns an integer (between 1 and 31) representing the day of the month for the specified (local time) date. Syntax: object.getDate( ) getDay Method This method returns an integer (0 for Sunday thru 6 for Saturday) representing the day of the week. Syntax: object.getDay( ) getFullYear Method This method returns an integer representing the year of a specified

Vous aimerez peut-être aussi