Javascript skills summary accumulated-(in progress)

Source: Internet
Author: User
Tags modulus set cookie

109. Change the status bar self. Status = "";//

110. Change the window size window. resizeTo (200,300 );//

111. Change the mouse style body {cursor: URL ('mouse. Ani '); SCROLLBAR-BASE-COLOR: #506aa8; SCROLLBAR-ARROW-COLOR: # 14213f ;}//

112. Transparent background <input type = "button" value = "button" style = "background-color: transparent; Border: 0;"> //

113. The mouse is in the waiting shape <input type = button onclick = "This. style. cursor = 'wait'"> //

114. Call the function opener. fucntion1 () of the parent window ();//

115. Internal HTML of the bodyCode<Input type = "button" onclick = "alert(code.doc ument. Body. innerhtml)" value = "View"> //

116. The Framework calls the function of the parent window <input type = 'button 'onclick = 'parent. Test (); 'value = 'Call the function of the parent window '> //

117. exchange node <Table width = 200 Height = 200 border> <tr> <TD id = C1> cell_1 </TD> </tr> <TD id = c2> cell_2 </TD> </tr> </table> <br> <input type = "button" value = "swap row" onclick = "c1.swapnode (C2) "> //

118. delete a node <Table width = 200 Height = 200 border> <tr id = trall> <TD id = C1> cell_1 </TD> </tr> <TD id = c2> cell_2 </TD> </tr> </table> <br> <input type = "button" value = "swap row" onclick = "trall. removenode (C2) "> //

119. Add node addnode ()//

120. Obtain the Parent and Child tags of the event. srcelement. Children [0] and event. srcelement. parentelement //

121. set the button to change the color <style> button {benc: expression (this. onfocus = function () {This. style. backgroundcolor = '# e5f0ff';}) }</style> <button> New </button> //

122. determine whether the left button or right-click is pressed <body onmousedown = If (event. button = 1) Alert ("Left click"); If (event. button = 2) Alert ("right-click")> //

123. Obtain the operating system name and the browser name document. Write (navigator. useragent )//

124. alt/CTRL/shift press event. altkey // press Alt key event. ctrlkey // Press ctrl key event. shiftkey // press shift

 

125. Locate the current position as drive C. {Window. Location = "C :"}//

126. Return the type of the input box <SCRIPT> alert (event. srcelement. type); // </SCRIPT>

127. Click the event <input type = "hidden" name = "guoguo" onclick = "Haha ()"> <script language = "JavaScript"> <! --

Function Haha () {alert ();} guoguo. Click (); // --> </SCRIPT> //

128. java. SQL. resultset rset = com.bsitc.util.dbassist.getit(.exe cutequery (querystatement, Conn); Java. SQL. resultsetmetadata metadata = rset. getmetadata (); int COUNT = metadata. getcolumncount (); string name = metadata. getcolumnname (I); string value = rset. getstring (I );//

129. format the number function format_number (STR, digit) {If (isnan (STR) {alert ("the value you passed in is not a number! "); Return 0;} else if (math. Round (DIGIT )! = Digit) {alert ("the number of decimal places you enter is not an integer! "); Return 0;} else return math. Round (parsefloat (STR) * Math. Pow (10, digit)/Math. Pow (10, digit );}

130. Press enter to convert it to the tab button if (event. keycode = 13) event. keycode = 9; // set

131. scroll bar <button onclick = "text1.scrolltop = text1.scrollheight"> scroll </button> <br> <textarea id = "text1" Cols = 50 rows = 10> 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 </textarea> //

132. Determine the object if (typeof (unknown) = "function") return true; If (typeof (unknown )! = "Object") return false ;//

133. Cancel the text box auto-completion function <input type = "text" AutoComplete = "off"> //

134. let the drop-down box automatically drop down <select onmouseover = "javascript: This. size = This. length "onmouseout =" javascript: This. size = 1 "> <option value =" "> 1 </option> <option value =" "> 2 </option> <option value =" "> 3 </Option> </SELECT> //

135. read the XML file var childrenobj = myselect // document. all. myselect; var oxmldoc = new activexobject ('msxml'); oxmldoc. url = "mymsg. XML "; var oroot = oxmldoc. root; If (oroot. children! = NULL) {for (VAR I = 0; I <oroot. children. item (0 ). children. length; ++ I) {oitem = oroot. children. item (0 ). children. item (I); ooption = New Option (oitem. text, oitem. value); childrenobj. add (ooption) ;}// mymsg. XML file <? XML version = "1.0" encoding = "gb2312"?> <Childrenlist> <AA> <child value = '000000'> Hangzhou </child>

<Child value = '000000'> Wenzhou </child>

</AA> <child value = '000000'> Taizhou </child>

<Child value = '000000'> Lishui region </child> </AA> </childrenlist> //

136. Click the picture to stop the image <a href = "javascript:"> </a> //

137. display Local Computer Information var wshnetwork = new activexobject ("wscript. network "); alert (" Domain = "+ wshnetwork. userdomain); alert ("computer name =" + wshnetwork. computername); alert ("user name =" + wshnetwork. username );//

138. Comparison time tdate = new date (, 35); // year, month, day, hour, minute ddate = new date (); tdate <ddate? Alert ("earlier than"): Alert ("later ");//

139. The link where the mouse is located is displayed. <body onmouseover = "If (event. srcelement. tagname = 'A') Alert (event. srcelement. href)"> <

Href = "http://51js.com/viewthread.php? Tid = 13589 "> dddd </a> <input> //

140. Note that you cannot compare it with undefined to test whether a variable exists, although you can check whether its type is "undefined ". In

In the following code example, assume thatProgramThe member wants to test whether the variable X: // has been declared. This method does not work. If (x = undefined) // some operations are performed. // This method also does not work.-Check required.

// String "undefined" If (typeof (x) = undefined) // for some operations // This method is valid if (typeof (x) = "undefined ") // perform some operations

141. create an object with certain attributes var myobject = new object (); myobject. name = "James"; myobject. age = "22"; myobject. phone = "555 1234 ";//

142. All attributes of an enumeration (loop) object for (var a in myobject) {// display "the property 'name' is James", and so on. Window. Alert ("the property" + A + "'is" + myobject [a]);} //

143. Determine whether a number is an integer var A = 23.2; alert (a % 1 = 1 )//

144. Create a date variable var a = new date (2000, 1, 1); alert (A. tolocaledatestring ());

145. define a new method function trim_1 () {return this. replace (/(^/S *) | (/S * $)/g, "");} string. prototype. trim = trim_1; alert ('cindy '. trim ());

146. define a function guoguo_date () {var tmp1, tmp2; tmp1 = This. getmonth () + 1 + ""; if (tmp1.length <2) tmp1 = "0" + tmp1; tmp2 = This. getdate () + ""; if (tmp2.length <2) tmp2 = "0" + tmp2; return this. getyear () + "-" + tmp1 + "-" + tmp2;} date. prototype. tolitestring = guoguo_date; alert (new date (). tolitestring ())

 

147. Pasta is a constructor with four parameters, defining objects. Function pasta (grain, width, shape, hasegg) {// what food is used? This. Grain = grain;

// How wide? (Value) This. width = width;

// What is the cross-sectional shape? (String) This. Shape = shape;

// Add the yolk? (Boolean) This. hasegg = hasegg;

// Definition method this. tostring = AA;} function AA () {;}// after defining the object constructor, use the new operator to create an object instance. VaR spaghetti = new pasta ("wheat", 0.2, "circle", true); var linguine = new pasta ("wheat", 0.3, "Oval", true ); // Add the defined attributes. Both spaghetti and linguine will automatically obtain the new attributes pasta. prototype. foodgroup = "carbohydrates ";

148. print out the cause of the error try {x = y // cause the error. } Catch (e) {document. Write (E. Description) // print "'y' is undefined ".}//

149. generate an Excel file and save var excelsheet; excelapp = new activexobject ("Excel. application "); excelsheet = new activexobject (" Excel. sheet "); // This code starts the application for creating objects (in this case, Microsoft Excel worksheet ). Once an object is created, you can use

Variables reference it in the code. In the following example, the object variable excelsheet is used to access the attributes and methods of the new object and other Excel objects,

Includes the application object and the activesheet. cells set. // Make Excel visible through the Application object. Excelsheet. application. Visible = true; // place some text in the first cell of the table. Excelsheet. activesheet. cells (1, 1). value = "this is column A, Row 1"; // Save the table. Excelsheet. saveas ("C: // test. xls"); // use the quit method of the Application object to close the Excel file. Excelsheet. application. Quit ();//

150. Obtain a group of objects var coll = Document. All. Tags ("Div"); If (Coll! = NULL) {for (I = 0; I <Coll. length; I ++ )...} // 151. print preview and print <object classid = "CLSID: 8856f961-340a-11d0-a96b-00c04fd705a2 "Height = 0 id = WB name = WB width = 0> </Object> <input type = button value = print preview onclick =" wb.exe CWB (7,1) "> <input type = button onclick = document. all. WB. execwb (6, 1) value = "print"> //

152. The object <input type = "text" name = "GG" value = AAAAA> <script language = "JavaScript"> <! -- Alert (document. All. GG. Value) // --> </SCRIPT> //

153. invalidate the scroll wheel function document. onmousewheel () {return false;} // 154. create a dialog box <script language = "jscript"> var opopup = Window. createpopup (); var opopupbody = opopup.doc ument. body; opopupbody. innerhtml = "display some <B> HTML </B> here. "; opopup. show (100,100,200, 50, document. body); </SCRIPT> //

155. Get the object var OBJ = Document. elementfrompoint (event. X, event. Y) where the mouse is located );//

156. Obtain the object on the left <input type = "text" name = "GG"> <input type = "text" name = "BB"

Onclick = "This. previussibling. value = 'guogo'"> //

157. navigate to document. all. hint_layer.style.left = event. X + document. body. scrollleft + 10; document. all. hint_layer.style.top = event. Y + document. body. scrolltop + 10 ;//

158. add the project var op = document. createelement ("option"); document. all. selected_items.children (index ). insertadjacentelement ("beforebegin", OP); OP. TEXT = document. all. all_items [I]. text; OP. value = document. all. all_items [I]. value ;//

159. checks whether a window has been opened. If yes, closes var A; if (a). close (); else a = Window. open ('','','');//

160. dynamically create a tag newelem = document. createelement ("Div"); newelem. id = "hint_layer"; document. body. appendchild (newelem); document. all. hint_layer.innertext = "guoguo ";//

161. Title Bar document. Title //

162. Background Image <body style = "background-Attachment: fixed" background = "img/bgfix.gif"> </body> // The background image does not move

<Style type = "text/CSS"> <! -- Body {background-image: img/bgchild.jpg; Background-position: center; Background-repeat: No-Repeat; Background-Attachment: fixed ;} --> </style> // center the background image

163. Set the transparent effect document. Form. XXX. Filters. Alpha. Opacity = 0 ~ 100 //

164. definition method var dragapproved = false; document. onmouseup = new function ("dragapproved = false"); // 165. function convertcurrency (currencydigits) {// constants: var maximum_number = 99999999999.99; // predefine the radix characters and currency symbols for output: vaR cn_zero = "zero"; var cn_one = "one"; var cn_two = "two"; var cn_three = "three"; var cn_four = "Si "; vaR cn_five = "Wu"; var cn_six = "Lu "; VaR cn_seven = "success"; var cn_eight = "success"; var cn_nine = "success"; var cn_ten = "pick"; var cn_hundred = "fail "; vaR cn_thousand = "canonical"; var cn_ten_thousand = ""; var cn_hundred_million = ""; var cn_symbol = "RMB"; var cn_dollar = "RMB "; vaR cn_ten_cent = "cent"; var cn_cent = "points"; var cn_integer = "whole"; // variables: var integral; // represent integral part of digit number. vaR decimal; // represent decimal part o F digit number. vaR outputcharacters; // The output result. vaR parts; var digits, radices, bigradices, Decimals; var zerocount; var I, P, D; var quotient, modulus; // validate input string: currencydigits = currencydigits. tostring (); If (currencydigits = "") {alert ("Empty input! "); Return" ";} If (currencydigits. Match (/[^,./d]/)! = NULL) {alert ("invalid characters in the input string! "); Return" ";} If (currencydigits ). match (/^ (/d {1, 3} (,/d {3 })*(. (/d {3},) */d {1, 3 }))?) | (/D + (./d + )?)) $/) = NULL) {alert ("illegal format of digit number! "); Return" ";} // normalize the format of input digits: currencydigits = currencydigits. replace (/,/g, ""); // remove comma delimiters. currencydigits = currencydigits. replace (/^ 0 +/, ""); // trim zeros at the beginning. // assert the number is not greater than the maximum number. if (number (currencydigits)> maximum_number) {alert ("too large a number to convert! "); Return" ";}// process the coversion from currency digits to characters: // separate integral and decimal parts before processing coversion: parts = currencydigits. split (". "); If (parts. length> 1) {integral = parts [0]; decimal = parts [1]; // cut down redundant decimal digits that are after the second. decimal = decimal. substr (0, 2);} else {integral = parts [0]; decimal = "";} // prepare the characters corresponding to the digits: digits = new array (cn_zero, cn_one, cn_two, cn_three, cn_four, cn_five, cn_six, cn_seven, cn_eight,

Cn_nine); radices = new array ("", cn_ten, cn_hundred, cn_thousand); bigradices = new array ("", cn_ten_thousand, digits); decimals = new array (cn_ten_cent, cn_cent); // start processing: outputcharacters = ""; // process integral part if it is larger than 0: If (number (Integral)> 0) {zerocount = 0; for (I = 0; I <integral. length; I ++) {P = integral. length-I-1; D = integral. substr (I, 1); quotient = P/4; modulus = P % 4; If (D = "0") {zerocount ++ ;} else {If (zerocount> 0) {outputcharacters + = digits [0];} zerocount = 0; outputcharacters + = digits [number (d)] + radices [modulus];} if (modulus = 0 & zerocount <4) {outputcharacters + = bigradices [quotient] ;}} outputcharacters + = cn_dollar;} // process decimal part if there is: if (decimal! = "") {For (I = 0; I <decimal. length; I ++) {d = decimal. substr (I, 1); If (D! = "0") {outputcharacters + = digits [number (d)] + decimals [I] ;}}// confirm and return the final output string: if (outputcharacters = "") {outputcharacters = cn_zero + cn_dollar;} If (decimal = "") {outputcharacters + = cn_integer;} outputcharacters = cn_symbol + outputcharacters; return outputcharacters ;}//

166. XML data island binding table

/ tr>
recipient sender topic content
end of table

// Cd_catalog.xml <? XML version = "1.0" encoding = "ISO-8859-1"?> <! -- Edited with XML spy v4.2 --> <catalog> <Cd> <title> empire burlesque </title> <artist> Bob Dylan </artist> <country> USA </country> <company> Columbia </company> <price> 10.90 </price> <year> 1985 </year> </Cd> <title> hide your heart </Title> <artist> Bonnie Taylor </artist> <country> UK </country> <company> CBS Records </company> <price> 9.90 </price> <year> 1988 </year> </Cd> <title> Greatest Hits </title> <artist> Dolly Parton </artist> <country> USA </country> <company> RCA </company> <price> 9.90 </price> <year> 1982 </year> </Cd> <title> still got the blues </title> <artist> Gary Moore </artist> <country> UK </country> <company> Virgin Records </company> <price> 10.20 </price> <year> 1990 </year> </Cd> </CATALOG> //

167. the following combination can correctly display Chinese characters ============================================== save in XML encoding XML page specify the encoding ansi gbk/GBK, gb2312 Unicode/Unicode UTF-8 UTF-8 ==================== ==========

168. XML operation <XML id = "xmldata" src = "/data/books. XML "> <Div id =" guoguo "> </div> <SCRIPT> var x = xmldata. recordset // obtain the record set if (X. absoluteposition <X. recordcount) // if the current absolute position is before the last record {X. movenext (); // move X. moveprevious (); // move forward X. absoluteposition = 1; // move to the first record X. absoluteposition = x. recordcount; // move to the last record. Pay attention to the record set X. absoluteposition is the number of records from 1 to the record set

Number of guoguo. innertext = xmldso. recordset ("field_name"); // retrieve a record from it} </SCRIPT>

169. Another way to dynamically modify CSS is this.runtimestyle.css text = "color: #990000; Border: 1px solid # cccccccc ";//

170. Regular Expressions matching Chinese characters: [/u4e00-/u9fa5]

Match double-byte characters (including Chinese characters): [^/x00-/xFF]

Application: Calculate the length of a string (two-byte length Meter 2, ASCII character meter 1)

String. Prototype. Len = function () {return this. Replace ([^/x00-/xFF]/g, "AA"). length ;}

Regular Expression for matching empty rows:/n [/S |] */R

Regular Expressions matching HTML tags:/<(. *)>. * </1> | <(. *)/>/

Regular Expression matching spaces at the beginning and end: (^/S *) | (/S * $)

Application: JavaScript does not have trim functions like VBScript. We can use this expression to implement it, as shown below:

String. Prototype. Trim = function () {return this. Replace (/(^/S *) | (/S * $)/g ,"");}

/////// Use a regular expression to break down and convert IP addresses:

The following is a javascript program that uses regular expressions to match IP addresses and convert IP addresses to corresponding values:

Function ip2v (IP) {Re =/(/d + )/. (/d + )/. (/d + )/. (/d +)/g // The regular expression that matches the IP address if (Re. test (IP) {return Regexp. $1 * Math. pow (255, 3) + Regexp. $2 * Math. pow (255, 2) + Regexp. $3*255 + Regexp. $4*1} else {Throw new error ("not a valid IP address! ")}}

However, if the above program does not use regular expressions, it may be easier to directly use the split function to separate them. The program is as follows:

VaR IP = "10.100.0000168" IP = IP. split (". ") Alert (" the IP value is: "+ (IP [0] * 255*255*255 + IP [1] * 255*255 + IP [2] * 255 + IP [3] * 1 ))

Match the regular expression of the email address:/W + ([-+.] /W +) * @/W + ([-.] /W + )*/. /W + ([-.] /W + )*

The regular expression matching the URL: http: // ([/W-] +/.) + [/W-] + (/[/W -./? % & =] *)?

///////// Use regular expressions to remove duplicate characters in the stringAlgorithmProgram:

VaR S = "abacabefgeeii" Var S1 = S. replace (/(.). */1/g, "$1") var Re = new Regexp ("[" + S1 + "]", "G") var S2 = S. replace (Re, "") Alert (S1 + S2) // The result is: abcefgi.

The idea is to use the back-to-back reference to retrieve repeated characters, then create a second expression with repeated characters, get non-repeated characters, and connect the two. This party

The method may not apply to strings with requirements for character order.

//////// Use a regular expression to extract the Javascript program with the file name from the URL. the following result is page1.

S = "http://www.9499.net/page1.htm" s = S. replace (/(. * //) {0,} ([^/.] + ). */ig, "$2") Alert (s)

//////// Use a regular expression to restrict text box input in a webpage form:

Use regular expressions to restrict Chinese characters only: onkeyup = "value = value. Replace (/[^/u4e00-/u9fa5]/g ,'')"

Onbeforepaste = "clipboardData. setdata ('text', clipboardData. getdata ('text'). Replace (/[^/u4e00-/u9fa5]/g ,'')

)"

Only full-width characters can be entered using regular expressions: onkeyup = "value = value. Replace (/[^/uff00-/Uffff]/g ,'')"

Onbeforepaste = "clipboardData. setdata ('text', clipboardData. getdata ('text'). Replace (/[^/uff00-/Uffff]/g ,'')

)"

Use a regular expression to limit that only numbers can be entered: onkeyup = "value = value. Replace (/[^/d]/g ,'')

"Onbeforepaste =" clipboardData. setdata ('text', clipboardData. getdata ('text'). Replace (/[^/d]/g ,''))"

Only numbers and English characters can be entered with a regular expression: onkeyup = "value = value. Replace (/[/W]/g ,'')

"Onbeforepaste =" clipboardData. setdata ('text', clipboardData. getdata ('text'). Replace (/[^/d]/g ,''))"

171. set and use cookies <HTML> <body> to set and read cookies... <br> write cookie value <input type = text name = Gg> <input type = button value = "set cookie" onclick = "set () "> <input type = button value =" read cookie "onclick =" Get () "> <br> <input type = text name = textbox> </body> <script language =" JavaScript "> function set () {var then = new date () then. settime (then. gettime () + 60*1000) // 60 seconds document. cookie = "cookie1 =" + GG. value + "; expires =" + then. togmtstring ()}

Function get () {var cookiestring = new string (document. Cookie) var cookieheader = "cookie1 =" Var beginposition = cookiestring. indexof (cookieheader) if (beginposition! =-1) {document. All. textbox. value = cookiestring. substring (beginposition + cookieheader. Length)} else document. All. textbox. value = "Cookie not found! "} </SCRIPT>

172. take the last day of the month function getlastday (year, month) {// take the year var new_year = year; // get the first day of the next month, note that the incoming month is from 1 ~ 12 var new_month = month ++; // if the current month is December, go to the next year if (month> 12) {new_month-= 12; new_year ++ ;} vaR new_date = new date (new_year, new_month, 1); Return (new date (new_date.gettime ()-1000*60*60*24 )). getdate ();}//

173. determine which of the Group's for (VAR I = 0; I <3; I ++) if (event. srcelement = BB [I]) break ;//

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.