Home >Web Front-end >JS Tutorial >The specific implementation of js to obtain the specified cookie_javascript skills

The specific implementation of js to obtain the specified cookie_javascript skills

WBOY
WBOYOriginal
2016-05-16 16:58:561236browse

Copy code The code is as follows:

var cookieName = "cookie name";
var cookieValue = null;//Return the value of the cookie
if (document.cookie && document.cookie != '') {
var cookies = document.cookie.split(';');//will get Cut all cookies into arrays
for (var i = 0; i < cookies.length; i ) {
var cookie = cookies[i];//Get the cookies array of a certain subscript
if ( cookie.substring(0, cookieName.length 2).trim() == cookieName.trim() "=") {//If the cookie exists, take out the cookie value
cookieValue = cookie.substring( cookieName.length 2, cookie.length);
break
}
}
}
if (cookieValue != "" && cookieValue != null) {//If the specified cookie exists Value
alert(cookieValue);
} else {//If the cookie value is empty
alert("not cookie!!!");
}

Copy code Code As follows:

//Remove spaces
String.prototype.trim = function () {
return this.replace(new RegExp("(^[\s]*)|( [\s]*$)", "g"), "");
}
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn