10 native tips for JavaScript sharing

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

This article to share is a personal summary of the 10 very common native JavaScript tips, are usually used in the project, here summarized record down, the need for small partners can refer to.

1, the implementation of string length interception

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19-20 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} else {break}} return Strre + "..."}

2, get the domain name host

?

1 2 3 4 5 6 7 8 9 10 11-12 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]; }

3, clear the space

?

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

4. Replace All

?

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

5. Escape HTML Tags

?

1 2 3 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, ' > ')

}

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}
1 2 3 4 5 6 7 8 9 a

8, to determine whether the number of types

?

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

9. Set cookie Value

?

1 2 3 4 5 6 7 8 9 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 * 60 * 60 * 1000); Document.cookie = name + "=" + Escape (value) + ";p ath=/;expires=" + exp.togmtstring () + ";d omain=360doc.com;"}

10. Get Cookie Value

?

1 2 3 4 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.