Register for your free account! | Forgot your password?

Go Back   elitepvpers > Coders Den > Coding Releases > Coding Snippets
You last visited: Today at 02:52

  • Please register to post and access all features, it's quick, easy and FREE!

Advertisement



[JS] Cookiemanagement

Discussion on [JS] Cookiemanagement within the Coding Snippets forum part of the Coding Releases category.

Reply
 
Old   #1

 
Ravenstorm's Avatar
 
elite*gold: 0
The Black Market: 100/0/0
Join Date: Jan 2010
Posts: 13,150
Received Thanks: 3,207
[JS] Cookiemanagement

Hey,

hier eine kleine Cookie-Verwaltung in JavaScript...

...ich habe diese 3 Funktionen vor längerem mal benutzt und letzte Woche mal überarbeitet und mit neuer Funktionalität versehen.

Code:
<script>
/**
     * Erstellung eines Cookies mit folgenden Attributen:
     *
     * name: Name des Cookies		
     * value: Wert des Cookies
     * expires (optional): Ablaufdatum des Cookies 
     * path (optional): Gültigkeitspfad (Standard: Aufrufendes Dokument)
     * domain (optional): Gültigkeitsdomain (Standard: Aufrufende Domain)
     * secureBool (optional): Bool ob sichere Übertragung für den Cookie gefordert ist
     */
    function createCookie(name, value, expires, path, domain, secureBool) {
		document.cookie= name + "=" + encodeURI(value) + 
		//Die optionalen Werte nur anfügen wenn sie gegeben sind
		((expires) ? "; expires=" + expires.toGMTString() : "") +
		((path) ? "; path=" + path : "") +
		((domain) ? "; domain=" + domain : "") +
		((secureBool) ? "; secure" : "");
    }
     
    /**
     * Wertauslese eines Cookies
     *
     * name: Name des gesuchten Cookies
     * @return: Gibt einen String mit dem Inhalt wieder, null wenn der Cookie nicht existiert
     */
    function getCookie(name) {
		var dc = document.cookie;
		var prefix = name + "=";
		var begin = dc.indexOf("; " + prefix);
		if (begin == -1) {
			begin = dc.indexOf(prefix);
			if (begin != 0) return null;
		} else {
			begin += 2;
		}
		var end = document.cookie.indexOf(";", begin);
		if (end == -1) {
			end = dc.length;
		}
		return decodeURI(dc.substring(begin + prefix.length, end));
    }
     
    /**
     * Löscht einen speziefischen Cookie
     *
     * name: Name des gesuchten Cookies
     * path (optional): Pfadangabe des Cookies, siehe createCookie()
     * domain (optional): Domainangabe des Cookies, siehe createCookie()
     */
    function deleteCookie(name, path, domain) {
		if (getCookie(name)) {
			document.cookie = name + "=" + ((path) ? "; path=" + path : "") + ((domain) ? "; domain=" + domain : "") +
			"; expires=Thu, 02-Jan-14 00:00:01 GMT";
		}
    }

</script>
Ravenstorm is offline  
Thanks
2 Users
Reply




All times are GMT +2. The time now is 02:52.


Powered by vBulletin®
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.
This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Support | Contact Us | FAQ | Advertising | Privacy Policy | Terms of Service | Abuse
Copyright ©2024 elitepvpers All Rights Reserved.