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 front and rear spaces   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 (); The event is re-executed;   6, convert  document.getelementbyid (ID) to $ ("id")       function $ (ID) {return typeof ( ID) = = ' String '? document.getElementById (ID): ID} 7, picture loading failed, and prevent dead loop   onerror= "This.src=aaa.jpg;this.onerror=null;"  8, document.getElementsByTagName (' * '). Length   View how many Dom elements are in the page;  9, parseint ()   returns only integral parts A complete parseint should look like this: parseint (string, radix), where radix specifies the number of decimal, binary, hexadecimal etc.   parseint ("F", 16): 15   Putting the plus sign in front of a string containing a valid number converts the string to a number, and the  null is represented as 0 when used as a number, and as a Boolean when the false.  declares a variable without assigning a value, the value of this variable is undefined. When undefined is used as a numberThe type behaves as Nan, and is used as a Boolean false. 10, a variety of regular validation rules   Digital 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 attribute 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 '); Show 18,24,28,30,38 in turn}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: Use student to execute the content of this class, in person this class exists this.name and so on, such as the statement,//so that the property is 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) >divh) {Console.log (' It's the fucking loading of the content.            ‘); }console.log (' page body full text High: ' + document.body.scrollHeight + ' page is rolled High: ' + document.body.scrollTop + ' screen usable workspace height: ' + window.scr Een.availheight);

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.