JS usage Tips-[tips on experience in recent years]

Source: Internet
Author: User

1. If Ajax returns a single JSON format, the receiver needs to format the assignment again: var str = eval ("(" + msg + ")");

Development reference:///<reference path= "Http://x.autoimg.cn/as/static/js/jquery-1.7.2.min.js"/>2, if Ajax with more than two requests, The returned status will be string, can not be used asynchronously, with synchronization to solve the problem; 3, navigator.plugins["Shockwave Flash"] Check the current machine Flash version 4, delete the space before and after String.prototype.trim = function () {return this.replace (/(^[|] *)| ([|] *$)/g, "");} 5, IE6 JS in the execution of a method, the current method after the execution of the code, the single thread will stop, the current method there are other methods will not be executed, if you want to execute, need to add setTimeout (); event re-execution; 6. Convert document.getElementById (ID) to $ ("id") function $ (ID) {return typeof (id) = = ' String '? document.getElementById (ID): ID}7, picture failed to load, and prevent dead loop onerror= "This.src=aaa.jpg;this.onerror=null;" 8, document.getElementsByTagName (' * '). length to see how many Dom elements the page has, 9, parseint () will only return an integer part; a complete parseint should look like this: parseint ( String, radix), where radix specifies the number of decimal, binary, hexadecimal etc. parseint ("F", 16): 15 The plus sign is preceded by a string containing a valid number, and the string is converted to a number, and Null is represented as 0 when used as a number. False when doing Boolean. Declares a variable without assigning a value, at which time the value of this variable is undefined. Undefined used as a number when the type behaves as Nan, and is used as a Boolean when it behaves as false.10, and various regular validation rulesDigital validation rules:"^\\d+$"//nonnegative integer (positive integer + 0) "^[0-9]*[1-9][0-9]*$"//Positive integer "^ ((-\\d+) | ( 0+) $ "//non-positive integer (negative integer + 0)" ^-[0-9]*[1-9][0-9]*$ "//Negative integer" ^-?\\d+$ "//Integer" ^\\d+ ("//non-negative floating-point number + 0)" ^ ([0-9]+\\.[ 0-9]*[1-9][0-9]*) | ([0-9]*[1-9][0-9]*\\. [0-9]+) | ([0-9]*[1-9][0-9]*)) $ "//positive floating point" ^ ((-\\d+ ("///non-positive floating-point number (negative float + 0)" ^ (-([0-9]+\\.[ 0-9]*[1-9][0-9]*) | ([0-9]*[1-9][0-9]*\\. [0-9]+) | ([0-9]*[1-9][0-9]*))) $ "//negative floating point" ^ (-?\\d+) ("//floating-point var r =/^\+?[  1-9][0-9]*$/; Positive integers
R.test (str); 11. Sort by JSON property valuesvar cc=[{name: "a", age:30},{name: "C", age:24},{name: "B", age:28},{name: "E", age:18},{name: "D", age:38}].sort (function (obj1, obj2) {return obj1.age-obj2.age;});For (Var i=0;i<cc.length;i++) {alert (cc[i][' age ');//Display 18,24,28,30,38}12. Combined array: var mergeto = [4,5,6];var mergefrom = [7,8,9]; Array.prototype.push.apply (mergeto, mergefrom); mergeto; is: [4, 5, 6, 7, 8, 9]13, multi-click event Gets the click of which $ (' #IndexLink, #IndexLink1 '). On (' click ', Function (e) {var id=e.target.id;
ID is the ID value to be clicked.
}14, Zepto inside or jquery back to the Ajax JSON string, if your JSON string is not canonical (inside the key name, value, must be double quotes), will jump out of the success method, and do not execute all the statements inside;15. In JavaScript, the following procedure declares a global variableUse declaring variables outside of function (whether or not Var is used), not declaring variables with Var in function, assigning directly to the Window property;16, Anchor point positioning methodWindow.location.hash = ' m001 '; <a name= "m001" >&nbsp;</a>17, Window.history.forward (1); Prevent the page from receding;18. JS Call and aplly usagefunction person (name, age) {this.name = Name;this.age = age;}; function Student (name, age, Grade) {person.apply (this, arguments); this.grade = grade;}; var student = new Student (' Qian ', 21, ' first grade '); alert (' Name: ' + student.name + ' \ n ' + ' Age: ' + student.age + ' \ n ' + ' Grade: ') + Student.grade);//That is, the popular point is: with student to carry out the contents of the person class, in the person this class exists this.name and other such statements,//so that the attributes are created in the student object 18) page to bottom automatically load content:var DIVH = Document.body.scrollheight,top = DOCUMENT.BODY.SCROLLTOP,WINDOWH = window.screen.availheight;if (top + WINDOWH) (&GT;DIVH) {Console.log (' It's time to load the fucking content up. ‘);} Console.log (' page body full text High: ' + document.body.scrollHeight + ' page is rolled High: ' + document.body.scrollTop + ' screen usable workspace height: ' + window.scre En.availheight);

Other wonderful articles

jquery Tutorial (-jquery) serialization form of AJAX operationsjquery Tutorial (-ajax) performing a POST request for Operationjquery Tutorial (-jquery) Ajax + PHP operation for AJAX requests to provide different ...jquery Tutorial (-jquery) Ajax callback functionjquery Tutorial (-ajax) error handling of operationsjquery Tutorial (-ajax) Ajax and events for Operation

more about Android Development articles


JS usage Tips-[tips on experience in recent years]

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.