Methods and attributes of common JavaScript objects (stolen)

Source: Internet
Author: User
Tags color representation string methods

Javascript is an object-based language. We know that an object is composed of a set of data (called an attribute in Javascript) and a method applied to this set of data. At the same time, there are some system functions in Javascript that do not belong to any object. This article will briefly introduce the attributes and methods of some common objects in Javascript, as well as several useful system functions.

I. String Method

Javascript has powerful string processing functions. With these string methods, you can compile a variety of web pages. This section describes how to use methods and attributes related to string objects.

1. Length attribute of the string object

The string object has only one attribute length, which indicates the relative number of characters contained in the string. Syntax: stringname. Length

2. Method of String object

Javascript provides multiple string methods to help control display information. String methods can be divided into several categories, such as attribute methods and navigation methods. Next we will introduce the categories.

(1) The attribute method of the attribute method string specifies the mode in which normal text is displayed on the screen. It provides an ideal way to make the Home Page more attractive.

The blink () Blink () method causes text flashing. The syntax format is as follows: String. Blink ();

The bold () method changes the string to bold. Syntax format: String. Bold ();

The fontcolor () method can change the color of a single string, or change the color each time a page or event is reloaded. Similarly, this method also has a corresponding HTML identifier. the syntax of fontcolor () is string. fontcolor (color). Here, color is a valid color representation.

The italics () method converts strings into italics, which are separated from other words. the syntax of italics () is as follows: String. Italics ();

(2) The case-sensitive method JavaScript strictly distinguishes uppercase and lowercase letters. To conveniently compare two strings, convert them to uppercase or lowercase letters. String objects can be converted between uppercase and lowercase.

The tolowercase () method converts each character in a given string to a lower-case state. Syntax format: String = stringvalue. tolowercase ();

The opposite of touppercase () and tolowercase () is the touppercase () method, which converts a given string into an uppercase string. String = stringvalue. touppercase ();

(3) operation method the operation method provides many methods for searching strings in a text string, including charat (), indexo F (), lastindexof (), and substring () four methods. We can use these methods to format and manipulate string variables entered by users. The charat () and substring () Methods Extract one or more characters from a string, the remaining two methods return the numeric value where the first character of the value to be searched appears in the string.

The charat () method returns the character at the specified position from a given string. Its usage is as follows: String. charat (INDEX); index is a number between 0 and less than the length of the string (the length of the string can be obtained through the Length attribute ),

The substring () method returns a substring of a large string. Its syntax is as follows: String. substring (Indexa, indexb) Indexa and indexb are both numerical values between 0 and the string length. They are two subscripts of a string.

The indexof () method returns the position of the child string that matches the specified string in a long string. It looks for the given string from left to right. The syntax format is as follows: string. indexof (searchvalue, [fromindex]); Where string is the string in which indexof () is to be searched for searchvalue, and searc hvalue is any character or substring to be searched. Fromindex is an option that specifies the start position of the search. The fromindex range is similar to charat () and subs tring () (0 to length-1 ). It cannot be greater than or equal to length. If fromindex is not specified, this method considers it to be 0 by default and searches for the entire string. The return value of indexof () is the offset of the first character that appears for the first time in searchvalue (starting from the beginning of the string ). Even if fromindex is specified, the returned value is still relative to the start of the string (the se archvalue may appear in the previous sections and will be ignored ).

The lastindexof () method is similar to the indexof () method, except that it searches for strings from right to left. Lastindexof () still gives the position of the first character of the string to be searched, rather than the position of the last character. The syntax of the lastindexof () method is as follows: String. Last indexof (searchvalue); searchvalue is still the string to be searched.

(4) When the navigation method uses hypertext, it will be exposed to the concept of anchor and link. An anchor is like a bookmark placed on a webpage (you can easily return to it); a link is a guide to another page (or an anchor of the same and different pages.

The anchor () method is used to set an anchor. Its syntax is simple: string. anchor (anchorname); Evaluate the following example: var indexstring = "Index"; document. writeln (indexstring. anchor ("anchorname"); this statement prints the text index on the screen; anchorname is the real name of the anchor. Such an anchor is generally used by links to jump to the actual location of the anchor in the page.

Link () is used to jump to a specified target. It is equivalent to standard HTML reference (hypertext reference specified by the href attribute ). Its Syntax format is also very simple: linktext. Link (href) linktext is the text on the screen, the href attribute is not necessarily an anchor, it can also be a legal URL reference. The following example shows how to use link () to create a link directly jump to the previously created anchor; var indexstirng = "Index" document. writeln (indexstring. link ("#" + "anchorname") +"

") This statement prints the text index of the link format on the screen (usually underlined below); anchorname is the actual name of the anchorname. When you click this link, the anchor appears at the top of the browser user window. Note: ensure that the # number is added to the reference of the anchor in the same document. When adding the # sign, enclose it in double quotation marks and use the plus sign to connect it with the complete reference. See the previous example. If # is omitted, the link target is a file rather than an anchor. Clicking this link will make the browser try to mount the file named anchorname (in this example. Because this file does not exist, the browser will generate the notfound error message.

Ii. Window Method

A window is the interface on which the browser interacts with the user. You can create a controllable window by using the window methods and properties in JavaScript. In this section, we will introduce some window methods and properties.

1. pre-defined window (dialog box)

Javascript provides three methods to generate a "simple" window (or dialog box): Alert (), confirm (), and promp T (). Although we cannot control the positions, buttons, or sizes of these dialogs, they provide a preliminary mechanism to prompt users or receive input. The methods are discussed below.

The Alert () method creates an independent message window. The syntax is as follows: Alert ("message") Where "message" is the text string to be displayed in the prompt window.

The confirm () Confirm () method is used to request user approval before an event is performed. It gives users the opportunity to stop an event. The syntax of this method is as follows: Confirm ("message") Where message is a text string.

The prompt () Prompt () method receives user feedback. You can enter information on the keyboard, or if they receive a default value, you can select with the mouse. The syntax of this method is as follows: prompt ("message") if you want to specify a default value, you can use: prompt ("message", imputdefault) Where "message" is a text or number. You can enter any information. Therefore, even if you want to enter a number for calculation, a variable may also be input as text. If you click the cancel key and the close key, pr ompt () returns a null value.

2. Custom window

Although the above three windows are simple, they have a common disadvantage that they are not flexible, that is, they cannot change their size and style. Next, we will discuss how to customize the window in JavaScript. First, we should know how to create a window.

The open () method creates a new window. Using this method to create a window is similar to selecting the new web bro solution of the File menu item. The syntax of this method is as follows: windowvar = Window. open ("url", "windowname") or you can define the properties of a window: windowvar = Window. open ("url", "windowname", "windowattribut Er") Where windowname is the name of the window to be created. A URL is a text string that specifies the URL to be called when a new window is created. If no URL is specified (using an empty string ""), the window is empty. Windowname indicates the name of the window. Windowfeatures is an optional attribute that can be assigned to a window, such as a toolbar, location, directory, status bar, menu bar, scroll bar, variable size, width, and height (see the attached table ). When setting them, use a comma as the separator and add double quotation marks to the outermost part.

Appendix

Window properties use the following attributes: feeture = value where feature is the property to be selected, value (except width and height) or yes (OR 1) to activate this property, or no (or 0) to remove it. The syntax of width and height is as follows: feature = pixels where pixels is any positive integer (or positive integer variable) to determine the size of the new window (in pixels ).

Close () Close the window using the close () method. Its syntax is as follows: windowreference. close () can use any of the following functions to close the current window: window. close (), self. close (), close (). We can close an independent window by referencing windowvar of a window (this value is generated when the open () method is called. For example, the command used to create a window is export WB = open (",", "anotherwindow", "windth = 80, Height = 40 ); we can close it in any other window and run the command: Export WB. close () to close the top-level window, you can use the following method to top. close () Of course, if you want to close the current window, you can use any self-closing command.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.