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

รวมโค้ด AJAX Javascript Library jQuery Framework ต่างๆ ที่ใช้ คู่กับ php Script Ajax โค้ด Ajax Prototype UI แนะนำได้ที่นี่

Moderators: mindphp, ผู้ดูแลกระดาน

User avatar
mindphp
ผู้ดูแลระบบ MindPHP
ผู้ดูแลระบบ MindPHP
Posts: 25161
Joined: 22/09/2008 6:18 pm
Contact:

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

Post by mindphp »

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
ติดตาม VDO: http://www.youtube.com/c/MindphpVideoman
ติดตาม FB: https://www.facebook.com/pages/MindphpC ... 9517401606
หมวดแชร์ความรู้: https://www.mindphp.com/forums/viewforum.php?f=29
รับอบรม และพัฒนาระบบ: https://www.mindphp.com/forums/viewtopic.php?f=6&t=2042

  • Similar Topics
    Replies
    Views
    Last post

Return to “AJAX Javascript Library - jQuery”

Who is online

Users browsing this forum: No registered users and 5 guests