Cookies in JavaScript: Set, Get & Beispiel löschen

Inhaltsverzeichnis:

Anonim

Was sind Cookies?

Ein Cookie ist ein Datenelement, das auf Ihrem Computer gespeichert wird und auf das Ihr Browser zugreifen kann. Möglicherweise haben Sie auch die Vorteile von Cookies wissentlich oder unwissentlich genossen. Haben Sie jemals Ihr Facebook-Passwort gespeichert, damit Sie es nicht jedes Mal eingeben müssen, wenn Sie versuchen, sich anzumelden? Wenn ja, verwenden Sie Cookies. Cookies werden als Schlüssel / Wert-Paare gespeichert.

Warum brauchst du ein Cookie?

Die Kommunikation zwischen einem Webbrowser und einem Server erfolgt über ein zustandsloses Protokoll namens HTTP. Das zustandslose Protokoll behandelt jede Anforderung unabhängig. Daher behält der Server die Daten nach dem Senden an den Browser nicht bei. In vielen Situationen werden die Daten jedoch erneut benötigt. Hier kommen Kekse ins Bild. Bei Cookies muss der Webbrowser nicht jedes Mal mit dem Server kommunizieren, wenn die Daten benötigt werden. Stattdessen kann es direkt vom Computer abgerufen werden.

Javascript Set Cookie

Sie können Cookies mithilfe eines Dokuments erstellen. Cookie-Eigenschaft wie diese.

document.cookie = "cookiename=cookievalue"

Sie können Ihrem Cookie sogar ein Ablaufdatum hinzufügen, damit das jeweilige Cookie zum angegebenen Datum vom Computer entfernt wird. Das Ablaufdatum sollte im UTC / GMT-Format eingestellt werden. Wenn Sie das Ablaufdatum nicht festlegen, wird das Cookie entfernt, wenn der Benutzer den Browser schließt.

document.cookie = "cookiename=cookievalue; expires= Thu, 21 Aug 2014 20:00:00 UTC"

Sie können auch die Domäne und den Pfad festlegen, um anzugeben, zu welcher Domäne und zu welchen Verzeichnissen in der bestimmten Domäne das Cookie gehört. Standardmäßig gehört ein Cookie zu der Seite, auf der das Cookie gesetzt wird.

document.cookie = "cookiename=cookievalue; expires= Thu, 21 Aug 2014 20:00:00 UTC; path=/ "

// Erstelle ein Cookie mit einer Domain zur aktuellen Seite und einem Pfad zur gesamten Domain.

JavaScript erhalten Cookie

Sie können wie folgt auf das Cookie zugreifen, das alle für die aktuelle Domain gespeicherten Cookies zurückgibt.

var x = document.cookie

JavaScript-Cookie löschen

Um ein Cookie zu löschen, müssen Sie nur den Wert des Cookies auf leer setzen und den Wert von expires auf ein überschrittenes Datum setzen.

document.cookie = "cookiename= ; expires = Thu, 01 Jan 1970 00:00:00 GMT"

Probieren Sie dieses Beispiel selbst aus:

 Cookie !!! </ title><script type = "text />Funktion createCookie (cookieName, cookieValue, daysToExpire){var date = neues Datum ();date.setTime (date.getTime () + (daysToExpire * 24 * 60 * 60 * 1000));document.cookie = cookieName + + cookieValue + "; expires =" + date.toGMTString ();}}Funktion accessCookie (cookieName){var name = cookieName + ;var allCookieArray = document.cookie.split (';');für (var i = 0; i <allCookieArray.length; i ++){var temp = allCookieArray [i] .trim ();if (temp.indexOf (name) == 0)return temp.substring (name.length, temp.length);}}Rückkehr "";}}Funktion checkCookie (){var user = accessCookie ("testCookie");if (user! = "")alert ("Willkommen zurück" + Benutzer + "!!!");sonst{user = prompt ("Bitte geben Sie Ihren Namen ein");num = prompt ("Wie viele Tage möchten Sie Ihren Namen auf Ihrem Computer speichern?");if (user! = "" && user! = null){createCookie ("testCookie", user, num);}}}}}}</ script></ head><body onload = "checkCookie ()"> </ body></ html></pre>
<p><strong> Spezielle Anweisungen, damit der Code funktioniert</strong></p>

<div id="container-5533149ee6411a5f99b370c693e87966"></div>
<strong>… Drücken Sie zweimal die Run-Taste</strong> <form class="hidden" code-box" name="sample"> Dieser Code kann bearbeitet werden. Klicken Sie zum Ausführen auf Ausführen<textarea name="codeguru"> </textarea>
<p><input type="submit" value="Lauf" style="background-color:#df5035;" color:#ffffff;"></p>
										</div>
									</article>
									
<div id="container-5533149ee6411a5f99b370c693e87966"></div>								</div>
							</div>
						</div>
					</div>
				</div>
				
<aside class="col-lg-4 sidebar sidebar--right">
	<div class="widget widget-popular-posts">
		<h2 class="widget-title">Beliebte Beiträge</h2>
		<ul class="post-list-small">
			<li class="post-list-small__item">
				<div class="post-list-small__entry clearfix">
					<div class="post-list-small__img-holder">
						<div class="thumb-container thumb-100"><a href="/8224749-cookies-in-jsp-with-example" title="Cookies in JSP mit Beispiel" rel="bookmark"><img src="https://cdn.css-code.org/4556956/cookies_in_jsp_with_example.png.webp" loading="lazy" alt="Cookies in JSP mit Beispiel" title="Cookies in JSP mit Beispiel" class="post-list-small__img--rounded" style="width:88px;height:88px" /></a></div>
					</div>
					<div class="post-list-small__body">
						<h3 class="post-list-small__entry-title"><a href="/8224749-cookies-in-jsp-with-example" title="Cookies in JSP mit Beispiel" rel="bookmark">Cookies in JSP mit Beispiel 2025</a></h3>
					</div>
				</div>
			</li><li class="post-list-small__item">
				<div class="post-list-small__entry clearfix">
					<div class="post-list-small__img-holder">
						<div class="thumb-container thumb-100"><a href="/8224750-jstl-jsp-standard-tag-library-tutorial-core-and-custom-tags-examples" title="JSTL-Tutorial (JSP Standard Tag Library): Core & Beispiele für benutzerdefinierte Tags" rel="bookmark"><img src="https://cdn.css-code.org/4144712/jstl_jsp_standard_tag_library_tutorial_core_ampamp_custom_tags_examples.png.webp" loading="lazy" alt="JSTL-Tutorial (JSP Standard Tag Library): Core & Beispiele für benutzerdefinierte Tags" title="JSTL-Tutorial (JSP Standard Tag Library): Core & Beispiele für benutzerdefinierte Tags" class="post-list-small__img--rounded" style="width:88px;height:88px" /></a></div>
					</div>
					<div class="post-list-small__body">
						<h3 class="post-list-small__entry-title"><a href="/8224750-jstl-jsp-standard-tag-library-tutorial-core-and-custom-tags-examples" title="JSTL-Tutorial (JSP Standard Tag Library): Core & Beispiele für benutzerdefinierte Tags" rel="bookmark">JSTL-Tutorial (JSP Standard Tag Library): Core & Beispiele für benutzerdefinierte Tags 2025</a></h3>
					</div>
				</div>
			</li><li class="post-list-small__item">
				<div class="post-list-small__entry clearfix">
					<div class="post-list-small__img-holder">
						<div class="thumb-container thumb-100"><a href="/8224752-jsp-form-processing-using-getparameter" title="JSP-Formularverarbeitung mit getParameter ()" rel="bookmark"><img src="https://cdn.css-code.org/7454960/jsp_form_processing_using_getparameter.png.webp" loading="lazy" alt="JSP-Formularverarbeitung mit getParameter ()" title="JSP-Formularverarbeitung mit getParameter ()" class="post-list-small__img--rounded" style="width:88px;height:88px" /></a></div>
					</div>
					<div class="post-list-small__body">
						<h3 class="post-list-small__entry-title"><a href="/8224752-jsp-form-processing-using-getparameter" title="JSP-Formularverarbeitung mit getParameter ()" rel="bookmark">JSP-Formularverarbeitung mit getParameter () 2025</a></h3>
					</div>
				</div>
			</li>
		</ul>
	</div>

	<div class="widget widget-popular-posts">
		<h2 class="widget-title">Die besten Bewertungen für den Monat</h2>
		<ul class="post-list-small">
			<li class="post-list-small__item">
				<div class="post-list-small__entry clearfix">
					<div class="post-list-small__body">
						<h3 class="post-list-small__entry-title"><a href="/8001126-trigger-click-on-input-when-label-is-clicked" title="Auslöser Klicken Sie auf Eingabe, wenn auf Beschriftung geklickt wird CSS-Tricks" rel="bookmark">Auslöser Klicken Sie auf Eingabe, wenn auf Beschriftung geklickt wird CSS-Tricks</a></h3>
					</div>
				</div>
			</li><li class="post-list-small__item">
				<div class="post-list-small__entry clearfix">
					<div class="post-list-small__body">
						<h3 class="post-list-small__entry-title"><a href="/8002860-align-self" title="Selbst ausrichten - CSS-Tricks" rel="bookmark">Selbst ausrichten - CSS-Tricks</a></h3>
					</div>
				</div>
			</li><li class="post-list-small__item">
				<div class="post-list-small__entry clearfix">
					<div class="post-list-small__body">
						<h3 class="post-list-small__entry-title"><a href="/8002861-all" title="Alle - CSS-Tricks" rel="bookmark">Alle - CSS-Tricks</a></h3>
					</div>
				</div>
			</li><li class="post-list-small__item">
				<div class="post-list-small__entry clearfix">
					<div class="post-list-small__body">
						<h3 class="post-list-small__entry-title"><a href="/8002862-animation" title="Animation - CSS-Tricks" rel="bookmark">Animation - CSS-Tricks</a></h3>
					</div>
				</div>
			</li><li class="post-list-small__item">
				<div class="post-list-small__entry clearfix">
					<div class="post-list-small__body">
						<h3 class="post-list-small__entry-title"><a href="/8002863-background" title="Hintergrund - CSS-Tricks" rel="bookmark">Hintergrund - CSS-Tricks</a></h3>
					</div>
				</div>
			</li><li class="post-list-small__item">
				<div class="post-list-small__entry clearfix">
					<div class="post-list-small__body">
						<h3 class="post-list-small__entry-title"><a href="/8002864-backdrop-filter" title="Hintergrundfilter - CSS-Tricks" rel="bookmark">Hintergrundfilter - CSS-Tricks</a></h3>
					</div>
				</div>
			</li><li class="post-list-small__item">
				<div class="post-list-small__entry clearfix">
					<div class="post-list-small__body">
						<h3 class="post-list-small__entry-title"><a href="/8002865-background-attachment" title="Hintergrund-Anhang - CSS-Tricks" rel="bookmark">Hintergrund-Anhang - CSS-Tricks</a></h3>
					</div>
				</div>
			</li><li class="post-list-small__item">
				<div class="post-list-small__entry clearfix">
					<div class="post-list-small__body">
						<h3 class="post-list-small__entry-title"><a href="/8002866-backface-visibility" title="Sichtbarkeit der Rückseite - CSS-Tricks" rel="bookmark">Sichtbarkeit der Rückseite - CSS-Tricks</a></h3>
					</div>
				</div>
			</li><li class="post-list-small__item">
				<div class="post-list-small__entry clearfix">
					<div class="post-list-small__body">
						<h3 class="post-list-small__entry-title"><a href="/8002867-appearance" title="Aussehen - CSS-Tricks" rel="bookmark">Aussehen - CSS-Tricks</a></h3>
					</div>
				</div>
			</li><li class="post-list-small__item">
				<div class="post-list-small__entry clearfix">
					<div class="post-list-small__body">
						<h3 class="post-list-small__entry-title"><a href="/8002868-background-clip" title="Hintergrund-Clip - CSS-Tricks" rel="bookmark">Hintergrund-Clip - CSS-Tricks</a></h3>
					</div>
				</div>
			</li><li class="post-list-small__item">
				<div class="post-list-small__entry clearfix">
					<div class="post-list-small__body">
						<h3 class="post-list-small__entry-title"><a href="/8002869-background-origin" title="Hintergrundherkunft - CSS-Tricks" rel="bookmark">Hintergrundherkunft - CSS-Tricks</a></h3>
					</div>
				</div>
			</li><li class="post-list-small__item">
				<div class="post-list-small__entry clearfix">
					<div class="post-list-small__body">
						<h3 class="post-list-small__entry-title"><a href="/8002871-background-color" title="Hintergrundfarbe - CSS-Tricks" rel="bookmark">Hintergrundfarbe - CSS-Tricks</a></h3>
					</div>
				</div>
			</li><li class="post-list-small__item">
				<div class="post-list-small__entry clearfix">
					<div class="post-list-small__body">
						<h3 class="post-list-small__entry-title"><a href="/8002872-background-image" title="Hintergrundbild - CSS-Tricks" rel="bookmark">Hintergrundbild - CSS-Tricks</a></h3>
					</div>
				</div>
			</li><li class="post-list-small__item">
				<div class="post-list-small__entry clearfix">
					<div class="post-list-small__body">
						<h3 class="post-list-small__entry-title"><a href="/8002873-background-position" title="Hintergrundposition - CSS-Tricks" rel="bookmark">Hintergrundposition - CSS-Tricks</a></h3>
					</div>
				</div>
			</li><li class="post-list-small__item">
				<div class="post-list-small__entry clearfix">
					<div class="post-list-small__body">
						<h3 class="post-list-small__entry-title"><a href="/8002874-background-repeat" title="Hintergrundwiederholung - CSS-Tricks" rel="bookmark">Hintergrundwiederholung - CSS-Tricks</a></h3>
					</div>
				</div>
			</li>
		</ul>
	</div>
	
<div id="container-5533149ee6411a5f99b370c693e87966"></div>	<div class="widget widget-popular-posts">
		<h2 class="widget-title">Top Artikel</h2>
		<ul class="post-list-small">
			<li class="post-list-small__item">
				<div class="post-list-small__entry clearfix">
					<div class="post-list-small__img-holder">
						<div class="thumb-container thumb-100"><a href="/8225671-how-to-make-balance-sheet-with-example" title="Wie erstelle ich eine Bilanz mit Beispiel?" rel="bookmark"><img src="https://cdn.css-code.org/3104803/how_to_make_balance_sheet_with_example.png.webp" loading="lazy" alt="Wie erstelle ich eine Bilanz mit Beispiel?" title="Wie erstelle ich eine Bilanz mit Beispiel?" class="post-list-small__img--rounded" style="width:88px;height:88px" /></a></div>
					</div>
					<div class="post-list-small__body">
						<h3 class="post-list-small__entry-title"><a href="/8225671-how-to-make-balance-sheet-with-example" title="Wie erstelle ich eine Bilanz mit Beispiel?" rel="bookmark">Wie erstelle ich eine Bilanz mit Beispiel? 2025</a></h3>
					</div>
				</div>
			</li><li class="post-list-small__item">
				<div class="post-list-small__entry clearfix">
					<div class="post-list-small__img-holder">
						<div class="thumb-container thumb-100"><a href="/8225672-20-best-small-business-accounting-software-in-usa-2021" title="20 BESTE Buchhaltungssoftware für kleine Unternehmen in den USA (2021)" rel="bookmark"><img src="https://cdn.css-code.org/3007815/20_best_small_business_accounting_software_in_usa_2021.png.webp" loading="lazy" alt="20 BESTE Buchhaltungssoftware für kleine Unternehmen in den USA (2021)" title="20 BESTE Buchhaltungssoftware für kleine Unternehmen in den USA (2021)" class="post-list-small__img--rounded" style="width:88px;height:88px" /></a></div>
					</div>
					<div class="post-list-small__body">
						<h3 class="post-list-small__entry-title"><a href="/8225672-20-best-small-business-accounting-software-in-usa-2021" title="20 BESTE Buchhaltungssoftware für kleine Unternehmen in den USA (2021)" rel="bookmark">20 BESTE Buchhaltungssoftware für kleine Unternehmen in den USA (2021) 2025</a></h3>
					</div>
				</div>
			</li><li class="post-list-small__item">
				<div class="post-list-small__entry clearfix">
					<div class="post-list-small__img-holder">
						<div class="thumb-container thumb-100"><a href="/8225673-65-best-accounting-courses-in-2021" title="65 beste Buchhaltungskurse im Jahr 2021" rel="bookmark"><img src="https://cdn.css-code.org/5693099/65_best_accounting_courses_in_2021.png.webp" loading="lazy" alt="65 beste Buchhaltungskurse im Jahr 2021" title="65 beste Buchhaltungskurse im Jahr 2021" class="post-list-small__img--rounded" style="width:88px;height:88px" /></a></div>
					</div>
					<div class="post-list-small__body">
						<h3 class="post-list-small__entry-title"><a href="/8225673-65-best-accounting-courses-in-2021" title="65 beste Buchhaltungskurse im Jahr 2021" rel="bookmark">65 beste Buchhaltungskurse im Jahr 2021 2025</a></h3>
					</div>
				</div>
			</li>
		</ul>
	</div>
</aside>			</div>
		</div>
		
<footer class="footer footer--dark">
	<div class="container">
		<div class="footer__widgets">
			<div class="row">
				<div class="col-lg-4">
					<div class="widget">
						<h2 class="widget-title">Beliebte Beiträge</h2>
						<ul class="post-list-small">
							<li class="post-list-small__item">
								<div>
									<div>
										<h3 class="post-list-small__entry-title"><a href="/8003260-11-getting-svg-the-noun-project" title="11: SVG bekommen - Das Nomen-Projekt - CSS-Tricks" rel="bookmark">11: SVG bekommen - Das Nomen-Projekt - CSS-Tricks</a></h3>
									</div>
								</div>
							</li><li class="post-list-small__item">
								<div>
									<div>
										<h3 class="post-list-small__entry-title"><a href="/8003261-12-getting-svg-icon-fonts-and-sets" title="12: SVG - Icon Fonts & Sets - CSS-Tricks" rel="bookmark">12: SVG - Icon Fonts & Sets - CSS-Tricks</a></h3>
									</div>
								</div>
							</li><li class="post-list-small__item">
								<div>
									<div>
										<h3 class="post-list-small__entry-title"><a href="/8003262-09-svg-with-data-uris" title="09: SVG mit Daten-URIs - CSS-Tricks" rel="bookmark">09: SVG mit Daten-URIs - CSS-Tricks</a></h3>
									</div>
								</div>
							</li><li class="post-list-small__item">
								<div>
									<div>
										<h3 class="post-list-small__entry-title"><a href="/8003263-13-svg-as-an-icon-system-the-use-element" title="13: SVG als Icon-System - Das `use`-Element - CSS-Tricks" rel="bookmark">13: SVG als Icon-System - Das `use`-Element - CSS-Tricks</a></h3>
									</div>
								</div>
							</li><li class="post-list-small__item">
								<div>
									<div>
										<h3 class="post-list-small__entry-title"><a href="/8003264-16-svg-icon-system-external-source" title="16: SVG-Symbolsystem - Externe Quelle - CSS-Tricks" rel="bookmark">16: SVG-Symbolsystem - Externe Quelle - CSS-Tricks</a></h3>
									</div>
								</div>
							</li>
						</ul>
					</div>
				</div>
				<div class="col-lg-4">
					<div class="widget">
						<h2 class="widget-title">Tipp Der Redaktion</h2>
						<ul class="post-list-small">
						<li class="post-list-small__item">
								<div>
									<div>
										<h3 class="post-list-small__entry-title"><a href="/8003243-23-templating-with-handlebars" title="# 23: Vorlagen mit Lenker - CSS-Tricks" rel="bookmark"># 23: Vorlagen mit Lenker - CSS-Tricks</a></h3>
									</div>
								</div>
							</li><li class="post-list-small__item">
								<div>
									<div>
										<h3 class="post-list-small__entry-title"><a href="/8003244-24-templating-with-underscore" title="# 24: Vorlagen mit Unterstrich - CSS-Tricks" rel="bookmark"># 24: Vorlagen mit Unterstrich - CSS-Tricks</a></h3>
									</div>
								</div>
							</li><li class="post-list-small__item">
								<div>
									<div>
										<h3 class="post-list-small__entry-title"><a href="/8003245-25-getting-organized" title="# 25: Organisieren! - CSS-Tricks" rel="bookmark"># 25: Organisieren! - CSS-Tricks</a></h3>
									</div>
								</div>
							</li><li class="post-list-small__item">
								<div>
									<div>
										<h3 class="post-list-small__entry-title"><a href="/8003246-26-using-a-jquery-plugin-from-the-wild" title="# 26: Verwenden eines jQuery-Plugins aus der Wildnis - CSS-Tricks" rel="bookmark"># 26: Verwenden eines jQuery-Plugins aus der Wildnis - CSS-Tricks</a></h3>
									</div>
								</div>
							</li><li class="post-list-small__item">
								<div>
									<div>
										<h3 class="post-list-small__entry-title"><a href="/8003248-27-building-a-simple-jquery-plugin" title="# 27: Erstellen eines einfachen jQuery-Plugins - CSS-Tricks" rel="bookmark"># 27: Erstellen eines einfachen jQuery-Plugins - CSS-Tricks</a></h3>
									</div>
								</div>
							</li>
						</ul>
					</div>
				</div>
				<div class="col-lg-4">
					<div class="widget">
						<h2 class="widget-title">Top Artikel</h2>
						<ul class="post-list-small">
						<li class="post-list-small__item">
								<div>
									<div>
										<h3 class="post-list-small__entry-title"><a href="/8003595-style-placeholder-text" title="Platzhaltertext formatieren - CSS-Tricks" rel="bookmark">Platzhaltertext formatieren - CSS-Tricks</a></h3>
									</div>
								</div>
							</li><li class="post-list-small__item">
								<div>
									<div>
										<h3 class="post-list-small__entry-title"><a href="/8003596-transparent-inside-border" title="Transparenter Innenrand - CSS-Tricks" rel="bookmark">Transparenter Innenrand - CSS-Tricks</a></h3>
									</div>
								</div>
							</li><li class="post-list-small__item">
								<div>
									<div>
										<h3 class="post-list-small__entry-title"><a href="/8003597-system-font-stack" title="Systemschriftstapel - CSS-Tricks" rel="bookmark">Systemschriftstapel - CSS-Tricks</a></h3>
									</div>
								</div>
							</li><li class="post-list-small__item">
								<div>
									<div>
										<h3 class="post-list-small__entry-title"><a href="/8003598-transparent-background-images" title="Transparente Hintergrundbilder - CSS-Tricks" rel="bookmark">Transparente Hintergrundbilder - CSS-Tricks</a></h3>
									</div>
								</div>
							</li><li class="post-list-small__item">
								<div>
									<div>
										<h3 class="post-list-small__entry-title"><a href="/8003600-top-shadow" title="Top Shadow - CSS-Tricks" rel="bookmark">Top Shadow - CSS-Tricks</a></h3>
									</div>
								</div>
							</li>
						</ul>
					</div>
				</div>
			</div>
			<p>© Copyright de.css-code.org, 2025 Juni | 
				<a href="https://de.css-code.org/about-site" title="Über Site">Über Site</a> | 
				<a href="https://de.css-code.org/contacts" title="Kontakte">Kontakte</a> | 
				<a href="https://de.css-code.org/privacy-policy" title="Datenschutz-Bestimmungen">Datenschutz-Bestimmungen</a>.
			</p>
			
		</div>
	</div>
</footer>		
<link href="https://css-code.org/template/css/style.min.css" rel="preload" as="style" onload="this.onload=null;this.rel='stylesheet'" />	</body>
</html>