Native JavaScript techniques collected 100 (I) (1)

Source: Internet
Author: User

1. Native JavaScript implements String Length Truncation

 
 
  1. function cutstr(str, len) { 
  2.     var temp; 
  3.     var icount = 0; 
  4.     var patrn = /[^\x00-\xff]/; 
  5.     var strre = ""; 
  6.     for (var i = 0; i < str.length; i++) { 
  7.         if (icount < len - 1) { 
  8.             temp = str.substr(i, 1); 
  9.             if (patrn.exec(temp) == null) { 
  10.                 icount = icount + 1 
  11.             } else { 
  12.                 icount = icount + 2 
  13.             } 
  14.             strre += temp 
  15.         } else { 
  16.             break 
  17.         } 
  18.     } 
  19.     return strre + "..." 

2. Obtain the domain name host using native JavaScript

 
 
  1. function getHost(url) { 
  2.     var host = "null"; 
  3.     if(typeof url == "undefined"|| null == url) { 
  4.         url = window.location.href; 
  5.     } 
  6.     var regex = /^\w+\:\/\/([^\/]*).*/; 
  7.     var match = url.match(regex); 
  8.     if(typeof match != "undefined" && null != match) { 
  9.         host = match[1]; 
  10.     } 
  11.     return host; 

3. Use native JavaScript to clear spaces

 
 
  1. String.prototype.trim = function() { 
  2.     var reExtraSpace = /^\s*(.*?)\s+$/; 
  3.     return this.replace(reExtraSpace, "$1") 

4. Replace all native JavaScript

 
 
  1. String.prototype.replaceAll = function(s1, s2) { 
  2.     return this.replace(new RegExp(s1, "gm"), s2) 

5. Native JavaScript escape html tags

 
 
  1. function HtmlEncode(text) { 
  2.     return text.replace(/&/g, '&').replace(/\"/g, '"').replace(/</g, '<').replace(/>/g, '>') 

7. Native JavaScript time and date format conversion

 
 
  1. Date. prototype. Format = function (formatStr ){
  2. Var str = formatStr;
  3. Var Week = ['day', 'yi', '2', '3', '4', '5', '6'];
  4. Str = str. replace (/yyyy | YYYY/, this. getFullYear ());
  5. Str = str. replace (/yy | YY/, (this. getYear () % 100)> 9? (This. getYear () % 100). toString (): '0' + (this. getYear () % 100 ));
  6. Str = str. replace (/MM/, (this. getMonth () + 1)> 9? (This. getMonth () + 1). toString (): '0' + (this. getMonth () + 1 ));
  7. Str = str. replace (/M/g, (this. getMonth () + 1 ));
  8. Str = str. replace (/w | W/g, Week [this. getDay ()]);
  9. Str = str. replace (/dd | DD/, this. getDate ()> 9? This. getDate (). toString (): '0' + this. getDate ());
  10. Str = str. replace (/d | D/g, this. getDate ());
  11. Str = str. replace (/hh | HH/, this. getHours ()> 9? This. getHours (). toString (): '0' + this. getHours ());
  12. Str = str. replace (/h | H/g, this. getHours ());
  13. Str = str. replace (/mm/, this. getMinutes ()> 9? This. getMinutes (). toString (): '0' + this. getMinutes ());
  14. Str = str. replace (/m/g, this. getMinutes ());
  15. Str = str. replace (/ss | SS/, this. getSeconds ()> 9? This. getSeconds (). toString (): '0' + this. getSeconds ());
  16. Str = str. replace (/s | S/g, this. getSeconds ());
  17. Return str
  18. }

8. Native JavaScript checks whether the data type is Numeric

 
 
  1. function isDigit(value) { 
  2.     var patrn = /^[0-9]*$/; 
  3.     if (patrn.exec(value) == null || value == "") { 
  4.         return false 
  5.     } else { 
  6.         return true 
  7.     } 

9. Set the cookie value in native JavaScript

 
 
  1. function setCookie(name, value, Hours) { 
  2.     var d = new Date(); 
  3.     var offset = 8; 
  4.     var utc = d.getTime() + (d.getTimezoneOffset() * 60000); 
  5.     var nd = utc + (3600000 * offset); 
  6.     var exp = new Date(nd); 
  7.     exp.setTime(exp.getTime() + Hours * 60 * 60 * 1000); 
  8.     document.cookie = name + "=" + escape(value) + ";path=/;expires=" + exp.toGMTString() + ";domain=360doc.com;" 

10. Obtain the cookie value using native JavaScript.

 
 
  1. function getCookie(name) { 
  2.     var arr = document.cookie.match(new RegExp("(^| )" + name + "=([^;]*)(;|$)")); 
  3.     if (arr != null) return unescape(arr[2]); 
  4.     return null 

11. Adding native JavaScript to favorites

 
 
  1. Function AddFavorite (sURL, sTitle ){
  2. Try {
  3. Window. external. addFavorite (sURL, sTitle)
  4. } Catch (e ){
  5. Try {
  6. Window. sidebar. addPanel (sTitle, sURL ,"")
  7. } Catch (e ){
  8. Alert ("failed to add to favorites, please add with Ctrl + D ")
  9. }
  10. }
  11. }

12. Set native JavaScript as the homepage

 
 
  1. Function setHomepage (){
  2. If (document. all ){
  3. Document. body. style. behavior = 'url (# default # homepage )';
  4. Document. body. setHomePage ('HTTP: // www.jq-school.com ')
  5. } Else if (window. sidebar ){
  6. If (window. netscape ){
  7. Try {
  8. Netscape. security. PrivilegeManager. enablePrivilege ("UniversalXPConnect ")
  9. } Catch (e ){
  10. Alert ("this operation is rejected by the browser. To enable this function, enter about: config in the address bar, and set the value of signed. applets. codebase_principal_support to true ")
  11. }
  12. }
  13. Var prefs = Components. classes ['@ mozilla.org/preferences-service%1'}.getservice (Components. interfaces. nsIPrefBranch );
  14. Prefs. setCharPref ('browser. startup. homepage', 'HTTP: // www.jq-school.com ')
  15. }
  16. }


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.