100 JavaScript Functional Code snippets that can be used directly

Source: Internet
Author: User
Tags set cookie



, native JavaScript implements string length interception


View Code





2. Native JavaScript gets the domain host


View Code





3. Native JavaScript Clear space


View Code



4. Native JavaScript replaces all


View Code



5. Native JavaScript Escape HTML tag


View Code



6. Native JavaScript to restore HTML tags


View Code



7. Native JavaScript time date format conversion


View Code



8, native JavaScript to determine whether the number type


View Code



9. Native JavaScript Set cookie value


View Code



10. Native JavaScript Gets the cookie value


View Code



11. Native JavaScript add to Favorite folder


View Code



12. Native JavaScript Set as homepage


View Code



13. Native JavaScript judgment IE6


View Code



14. Native JavaScript load Style file


 1 function LoadStyle(url) {
 2         try {
 3             document.createStyleSheet(url)
 4         } catch(e) {
 5             var cssLink = document.createElement(‘link‘);
 6             cssLink.rel = ‘stylesheet‘;
 7             cssLink.type = ‘text/css‘;
 8             cssLink.href = url;
 9             var head = document.getElementsByTagName(‘head‘)[0];
10             head.appendChild(cssLink)
11         }
12     }





15. Native JavaScript returns script content


 1 function evalscript(s) {
 2         if(s.indexOf(‘<script‘) == -1) return s;
 3         var p = /<script[^\>]*?>([^\x00]*?)<\/script>/ig;
 4         var arr = [];
 5         while(arr = p.exec(s)) {
 6             var p1 = /<script[^\>]*?src=\"([^\>]*?)\"[^\>]*?(reload=\"1\")?(?:charset=\"([\w\-]+?)\")?><\/script>/i;
 7             var arr1 = [];
 8             arr1 = p1.exec(arr[0]);
 9             if(arr1) {
10                 appendscript(arr1[1], ‘‘, arr1[2], arr1[3]);
11             } else {
12                 p1 = /<script(.*?)>([^\x00]+?)<\/script>/i;
13                 arr1 = p1.exec(arr[0]);
14                 appendscript(‘‘, arr1[2], arr1[1].indexOf(‘reload=‘) != -1);
15             }
16         }
17         return s;
18     }





16. Native JavaScript clears script content


View Code





17. Native JavaScript dynamic load script file


1 function appendscript(src, text, reload, charset) {
 2         var id = hash(src + text);
 3         if(!reload && in_array(id, evalscripts)) return;
 4         if(reload && $(id)) {
 5             $(id).parentNode.removeChild($(id));
 6         }
 7      
 8         evalscripts.push(id);
 9         var scriptNode = document.createElement("script");
10         scriptNode.type = "text/javascript";
11         scriptNode.id = id;
12         scriptNode.charset = charset ? charset : (BROWSER.firefox ? document.characterSet : document.charset);
13         try {
14             if(src) {
15                 scriptNode.src = src;
16                 scriptNode.onloadDone = false;
17                 scriptNode.onload = function () {
18                     scriptNode.onloadDone = true;
19                     JSLOADED[src] = 1;
20                 };
21                 scriptNode.onreadystatechange = function () {
22                     if((scriptNode.readyState == ‘loaded‘ || scriptNode.readyState == ‘complete‘) && !scriptNode.onloadDone) {
23                         scriptNode.onloadDone = true;
24                         JSLOADED[src] = 1;
25                     }
26                 };
27             } else if(text){
28                 scriptNode.text = text;
29             }
30             document.getElementsByTagName(‘head‘)[0].appendChild(scriptNode);
31         } catch(e) {}
32     }





18. Native JavaScript returns the element object retrieved by ID


View Code





19. Native JavaScript returns browser version content


View Code





20. Common method for native JavaScript element display


View Code





21, native JavaScript has InsertBefore method, but unfortunately there is no InsertAfter method? Use the following function to implement


View Code





22. Native JavaScript compatible with browser binding element events


View Code





23. The native JavaScript cursor pauses behind the text and is called when the text box gets focus


View Code





24. Native JavaScript Verify URL link is valid


View Code





25. Native JavaScript formatting CSS style code


View Code





26. Native JavaScript compression CSS style code


View Code





27. Native JavaScript Gets the current path


View Code





28, the original javascriptip into an integral type


View Code





29, native javascript integer type resolves to IP address


View Code





30, Native JavaScript implementation checkbox Select All and do not select


View Code





31, native JavaScript to determine whether mobile devices


View Code





32, native JavaScript to determine whether mobile device access


View Code





33. Native JavaScript to determine if Apple mobile device access


View Code





34. Native JavaScript to determine if Android mobile device access


View Code



35. Native JavaScript to determine if touch screen


View Code





36. Native JavaScript to determine whether the Google browser on Android


View Code



37, native JavaScript to determine whether to open the window


View Code






38. Native JavaScript Gets the size of the mobile device initialization


View Code






39. Native JavaScript Gets the maximum size of the mobile device


View Code






40. Native JavaScript Gets the mobile device screen width


View Code






41, native JavaScript perfect judge whether the URL


View Code






42. Native JavaScript retrieves element objects based on style name


View Code





43. Native JavaScript determines whether to start with a string


View Code



44. Native JavaScript determines whether to end with a string


View Code






45. Native JavaScript Returns the version number of IE browser


View Code






46. Native JavaScript Gets the page height


View Code






47. Native JavaScript Get page scrollleft


View Code






48. Native JavaScript Gets the page visual width


View Code






49. Native JavaScript Gets the page width


View Code






50. Native JavaScript Get page scrolltop


View Code






51. Native JavaScript Gets the page visual height


View Code






52. Native JavaScript adds events across browsers


View Code






53. Native JavaScript cross-browser Delete event


View Code






54. Native JavaScript removes URL prefixes


View Code






55. Native JavaScript random number timestamp


View Code






56, native JavaScript full-width half-width conversion, icase:0 full to half, 1 half to full, others do not convert


View Code






57. Native JavaScript to confirm that the keyboard is valid input value


View Code






58. Native JavaScript Gets the location where the page is being rolled


View Code






59. Native JavaScript Another regular date format function + Call method


View Code






60. Native JavaScript time personalized output function


View Code






61. Native javascript resolves offsetx compatibility issues


View Code






62. Regular expressions commonly used by native JavaScript


View Code






63, native JavaScript implementation return to the top of the general method


View Code






64. Native JavaScript Gets the value of the get parameter in the URL


View Code






65, native JavaScript implementation of the Universal method of selecting all


View Code






66, native JavaScript implementation of all the common method of de-selection


View Code






67. Native JavaScript implementation open a form common method


View Code






68, native JavaScript to determine whether the client device


View Code






69. Native JavaScript Gets the value of the radio button


View Code






70. Native JavaScript Gets the value of the check box


View Code






71, native JavaScript to determine whether the mailbox


View Code






72. Native JavaScript to determine if there are dangerous characters in the list


View Code






73, native JavaScript to determine whether the string is greater than the specified length


View Code






74. Native JavaScript judgment string is case-insensitive for URLs


View Code






75, native JavaScript to determine whether the string is a decimal


View Code






76. Native JavaScript determines whether a string is an integer


View Code






77. Native JavaScript Determines whether a string is a floating-point number


View Code






78, native JavaScript judge whether the character is a-za-z English letter


View Code






79. Native JavaScript to determine whether the string is postal code


View Code






80. Native JavaScript determines if the character is empty null


View Code






81. Native JavaScript uses regular expressions to extract all URLs in the page code


View Code






82. Native JavaScript uses regular expressions to clear the same array (inefficient)


View Code






83. Native JavaScript uses regular expressions to clear the same array (high efficiency)


View Code






84. Native JavaScript uses regular expressions to sort the array by alphabetical order for each row


View Code






85. Native JavaScript string reverse order


View Code






86. Native JavaScript uses regular expressions to clear scripts in HTML code


View Code






87. Native JavaScript dynamically executes JavaScript scripts


View Code






88. Native JavaScript dynamically executes VBScript scripts


View Code






89, native JavaScript implementation amount capitalization conversion function


View Code






90. Large collection of regular expressions commonly used by native JavaScript


View Code






91, native JavaScript implementation of the form Change event resize operation (compatible with the browser)


View Code





92, native JavaScript with regular clear space around


View Code





93, native JavaScript to determine whether the variable is null value


View Code





94. Native JavaScript implements Base64 decoding


View Code





95. Native JavaScript implements UTF8 decoding


View Code





96. Native JavaScript Gets the width and height of the form's visible range


View Code





97, native JavaScript judge ie version number (both concise and backward compatible!) )


View Code





98. Native JavaScript Gets the browser version number


View Code





99. Native JavaScript half-width conversion to full-width function


View Code





100. Native JavaScript full-width conversion to half-width function


View Code





100 JavaScript Functional Code snippets that can be used directly


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.