ลบ Cookies ทั้งหมด ของโดเมนด้วย Javascript

Post a reply


In an effort to prevent automatic submissions, we require that you complete the following challenge.
Smilies
:icon_plusone: :like: :plusone: :gfb: :-D :) :( :-o 8O :? 8) :lol: :x :P :oops: :cry: :evil: :twisted: :roll: :wink: :!: :?: :idea: :arrow: :| :mrgreen: :angry: :baa: :biggrin:
View more smilies

BBCode is ON
[img] is ON
[flash] is OFF
[url] is ON
Smilies are ON

Topic review
   

Expand view Topic review: ลบ Cookies ทั้งหมด ของโดเมนด้วย Javascript

ลบ Cookies ทั้งหมด ของโดเมนด้วย Javascript

by mindphp » 27/07/2009 9:02 pm

Code: Select all

DP_Cookies = new Object();

DP_Cookies.enabled = function() {

	return navigator.cookieEnabled

};

DP_Cookies.set = function(Name, Value, Timespan, TimespanUnit, Domain, Path, Secure) {

		// Set the Name
	Name = Name.replace(/^\s*|\s*$/g,"");
		// Set the Value
	if ( Value ) { Value = escape(Value) } else { Value="" };
		// Set the Expires Value
	if ( typeof Timespan == "number" ) {
		var CurDate = new Date();
		switch (TimespanUnit) {
			case "days":
				Timespan = Timespan * 24 * 60 * 60 * 1000;
				break;
			case "hours":
				Timespan = Timespan * 60 * 60 * 1000;
				break;
			case "minutes":
				Timespan = Timespan * 60 * 1000;
				break;
			case "seconds":
				Timespan = Timespan * 1000;
				break;
			default:
				Timespan = Timespan * 24 * 60 * 60 * 1000;
				break;
		};
		CurDate.setTime(CurDate.getTime() + Timespan);
		var Expires = "; expires=" + CurDate.toGMTString();
	} else {
		var Expires = "";
	};
		// Set the Path value
	if ( Path ) { Path = "; path=" + Path } else { Path = "" };
		// Set the Domain value
	if ( Domain ) { Domain = "; domain=" + Domain } else { Domain = "" };
		// Set the Secure value
	if ( Secure ) { Secure = "; secure" } else { Secure = "" };

	var CookieValue = Name + "=" + Value + Expires + Path + Domain + Secure;

		// Write the Cookie
	document.cookie = CookieValue;

		// Return the cookie
	return CookieValue;

};

DP_Cookies.erase = function(Name) {

		// Delete the cookie by calling "set" with a negative expiry value
	this.set(Name.replace(/^\s*|\s*$/g,""), "", -1);

		// Return "true"
	return true;

};

DP_Cookies.eraseAll = function() {

	var Cookies = document.cookie.split(";");

	for ( var Cnt=0; Cnt < Cookies.length; Cnt++ ) {
		var CurCookie = Cookies[Cnt].split("=");
		if ( CurCookie[0] ) {
			this.set(CurCookie[0].replace(/^\s*|\s*$/g,""), "", -1);
		};
	};

		// Return "true"
	return true;

};

DP_Cookies.get = function(Name) {

		// Set the Name
	Name = Name.replace(/^\s*|\s*$/g,"");

	var Cookies = document.cookie.split(";");

	for ( var Cnt=0; Cnt < Cookies.length; Cnt++ ) {
		var CurCookie = Cookies[Cnt].split("=");
		if ( CurCookie[0].replace(/^\s*|\s*$/g,"") == Name ) {
			if ( CurCookie[1] ) {
				return CurCookie[1];
			} else {
				return "";
			};
		};
	};

	return null;

};

DP_Cookies.getAll = function() {

	var AllCookies = new Object;
	var Cookies = document.cookie.split(";");

	for ( var Cnt=0; Cnt < Cookies.length; Cnt++ ) {
		var CurCookie = Cookies[Cnt].split("=");
		if ( CurCookie[0] ) {
			if ( CurCookie[1] ) {
				AllCookies[CurCookie[0].replace(/^\s*|\s*$/g,"")] = CurCookie[1];
			} else {
				AllCookies[CurCookie[0].replace(/^\s*|\s*$/g,"")] = "";
			};
		};
	};

	return AllCookies;

};
เรียกใช้โดย

Code: Select all

DP_Cookies.eraseAll();
ที่มาจาก: depressedpress.com

Top