Share 10 native JavaScript tips _javascript tips

Source: Internet
Author: User
Tags getdate html tags set cookie

1, the implementation of string length interception

 function Cutstr (str, len) {
   var temp;
   var icount = 0;
   var patrn =/[^\x00-\xff]/;
   var Strre = "";
   for (var i = 0; i < str.length i++) {
     if (Icount < len-1) {
       temp = str.substr (i, 1);
       if (patrn.exec (temp) = = null) {
         icount = icount + 1
       } else {
         icount = icount + 2
       }
       Strre + = temp< c15/>} else {break}} return
   Strre + "..."
 }

2, get the domain name host

 function GetHost (URL) {
   var host = ' null ';
   if (typeof url = = "undefined" | | | null = = URL) {
     url = window.location.href;
   }
   var regex =/^\w+\:\/\/([^\/]*). * *;
   var match = Url.match (regex);
   if (typeof match!= "undefined" && null!= match) {
     host = match[1];
   }
   return host;
 }

3, clear the space

 String.prototype.trim = function () {
   var reextraspace =/^\s* (. *?) \s+$/;
   Return This.replace (Reextraspace, "$")
 }

4. Replace All

 String.prototype.replaceAll = function (S1, S2) {return
   this.replace (new RegExp (S1, "GM"), S2)
 }

5. Escape HTML Tags

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

6. Restore HTML tags

Copy Code code as follows:

function HtmlDecode (text) {
Return Text.replace (/&/g, ' & '). Replace (/"/g, ' \"). Replace (/</g, ' < '). Replace (/>/g, ' > ')
}

7, Time date format conversion

 Date.prototype.Format = function (formatstr) {var str = FORMATSTR;
   var Week = [' Day ', ' one ', ' two ', ' three ', ' four ', ' five ', ' six ']; str = str.replace (/yyyy|
   yyyy/, This.getfullyear ()); str = str.replace (/yy| yy/, (this.getyear ()%) > 9?
   (This.getyear ()%). ToString (): ' 0 ' + (this.getyear ()% 100)); str = str.replace (/mm/, This.getmonth () + 1) > 9?
   (This.getmonth () + 1). ToString (): ' 0 ' + (this.getmonth () + 1));
   str = str.replace (/m/g, (This.getmonth () + 1)); str = str.replace (/w|
   W/g, Week[this.getday ()]); str = str.replace (/dd| Dd/, This.getdate () > 9?
   This.getdate (). toString (): ' 0 ' + this.getdate ()); str = str.replace (/d|
   D/g, This.getdate ()); str = str.replace (/hh| hh/, This.gethours () > 9?
   This.gethours (). toString (): ' 0 ' + this.gethours ()); str = str.replace (/h|
   H/g, This.gethours ());
   str = str.replace (/mm/, This.getminutes () > 9 this.getminutes (). toString (): ' 0 ' + this.getminutes ());
   str = str.replace (/m/g, This.getminutes ()); STR = str.replace (/ss| ss/, This.getseconds () > 9?
   This.getseconds (). toString (): ' 0 ' + this.getseconds ()); str = str.replace (/s|
   S/g, This.getseconds ());

 Return STR}

8, to determine whether the number of types

 function IsDigit (value) {
   var patrn =/^[0-9]*$/;
   if (patrn.exec (value) = NULL | | value = = "") {return
     false
   } else {return
     true
   }
 }

9. Set cookie Value

 function Setcookie (name, value, Hours) {
   var d = new Date ();
   var offset = 8;
   var UTC = D.gettime () + (D.gettimezoneoffset () * 60000);
   var nd = UTC + (3600000 * offset);
   var exp = new Date (nd);
   Exp.settime (Exp.gettime () + Hours * * * 1000);
   Document.cookie = name + "=" + Escape (value) + ";p ath=/;expires=" + exp.togmtstring () + ";d omain=360doc.com;"
 }

10. Get Cookie Value

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

The above mentioned is the entire content of this article, I hope you can enjoy.

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.