Share 10 native JavaScript tips __java

Source: Internet
Author: User
Tags getdate set cookie

First of all here to be very grateful to share the work of the netizens, these code snippets mainly by the users usually share the work code inside and often go to visit the site and then view the source files collected. The usual site on the common use of a number of functional code snippets all collected, convenient for users to learn the usage, the use of good words can speed up the development of netizens speed, improve efficiency.

1, native JavaScript implementation string length intercept

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 < le n-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, native JavaScript acquisition domain name host

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

3, native JavaScript clear space

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

4. Native JavaScript replaces all

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

5. Native JavaScript escape HTML tags

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

6. Native JavaScript restore HTML tags

function HtmlDecode (text) {return
text.replace (/&amp;/g, ' & '). Replace (/&quot;/g, ' \ "). Replace (/& amp;lt;/g, ' < '). Replace (/&gt;/g, ' > ')
}

7. Native JavaScript 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. Native JavaScript Determines whether it is a numeric type

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

9, native JavaScript 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=x.com;"
}

10. Native JavaScript gets cookie value

function GetCookie (name) {
var arr = document.cookie.match (New RegExp ("(^|)" + name + "= ([^;]    *)(;|$)"));
if (arr!= null) return unescape (arr[2));
return null
}
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.