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="/8003056-text-shadow" title="Textschatten - CSS-Tricks" rel="bookmark"><img src="https://cdn.css-code.org/9022028/text-shadow_css-tricks.png.webp" loading="lazy" alt="Textschatten - CSS-Tricks" title="Textschatten - CSS-Tricks" 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="/8003056-text-shadow" title="Textschatten - CSS-Tricks" rel="bookmark">Textschatten - CSS-Tricks 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="/8003057-text-overflow" title="Textüberlauf - CSS-Tricks" rel="bookmark"><img src="https://cdn.css-code.org/5435238/text-overflow_css-tricks.png.webp" loading="lazy" alt="Textüberlauf - CSS-Tricks" title="Textüberlauf - CSS-Tricks" 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="/8003057-text-overflow" title="Textüberlauf - CSS-Tricks" rel="bookmark">Textüberlauf - CSS-Tricks 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="/8003058-scroll-margin" title="Bildlaufrand - CSS-Tricks" rel="bookmark"><img src="https://cdn.css-code.org/5946511/scroll-margin_css-tricks.png.webp" loading="lazy" alt="Bildlaufrand - CSS-Tricks" title="Bildlaufrand - CSS-Tricks" 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="/8003058-scroll-margin" title="Bildlaufrand - CSS-Tricks" rel="bookmark">Bildlaufrand - CSS-Tricks 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="/8224218-how-to-audit-payroll-in-sap-rpuaud00" title="So prüfen Sie die Personalabrechnung in SAP: RPUAUD00" rel="bookmark">So prüfen Sie die Personalabrechnung in SAP: RPUAUD00</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="/8224219-sap-wage-types-tutorial-primary-secondary-dialog-time" title="Tutorial zu SAP-Lohnarten: Primär, Sekundär, Dialog, Zeit" rel="bookmark">Tutorial zu SAP-Lohnarten: Primär, Sekundär, Dialog, Zeit</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="/8224220-how-to-execute-payroll-in-sap" title="So führen Sie die Personalabrechnung in SAP aus" rel="bookmark">So führen Sie die Personalabrechnung in SAP aus</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="/8224223-sap-fi01-how-to-create-a-bank-key" title="SAP FI01: So erstellen Sie einen BANKSCHLÜSSEL" rel="bookmark">SAP FI01: So erstellen Sie einen BANKSCHLÜSSEL</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="/8224224-sap-wage-type-reporter-pc00-mxx-cwtr" title="SAP Wage Type Reporter: PC00_MXX_CWTR" rel="bookmark">SAP Wage Type Reporter: PC00_MXX_CWTR</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="/8224225-top-sap-hr-interview-questions" title="Die wichtigsten Fragen zum SAP HR-Interview" rel="bookmark">Die wichtigsten Fragen zum SAP HR-Interview</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="/8224227-bank-transfer-using-sap-dme-file" title="Banküberweisung mit SAP DME File" rel="bookmark">Banküberweisung mit SAP DME File</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="/8224228-sap-hr-module-pdf-hcm-and-payroll-overview-download-now" title="SAP HR Modul PDF: HCM & Gehaltsabrechnung (Jetzt herunterladen)" rel="bookmark">SAP HR Modul PDF: HCM & Gehaltsabrechnung (Jetzt herunterladen)</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="/8224229-overview-of-sap-mm-module" title="Übersicht über das SAP MM-Modul" rel="bookmark">Übersicht über das SAP MM-Modul</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="/8224231-sap-ad-hoc-query-tutorial-s-ph0-48000513" title="SAP AD HOC Query Tutorial (S_PH0_48000513)" rel="bookmark">SAP AD HOC Query Tutorial (S_PH0_48000513)</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="/8224232-how-to-create-material-master-data-mm01-in-sap" title="So erstellen Sie Materialstammdaten MM01 in SAP" rel="bookmark">So erstellen Sie Materialstammdaten MM01 in SAP</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="/8224233-how-to-change-material-master-data-mm02-mm03-in-sap" title="So ändern Sie Materialstammdaten (MM02, MM03) in SAP" rel="bookmark">So ändern Sie Materialstammdaten (MM02, MM03) in SAP</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="/8224234-introduction-to-master-data-in-sap" title="Einführung in Stammdaten in SAP" rel="bookmark">Einführung in Stammdaten in SAP</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="/8224235-how-to-copy-material-master-in-sap-mm01" title="So kopieren Sie den Materialstamm in SAP: MM01" rel="bookmark">So kopieren Sie den Materialstamm in SAP: MM01</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="/8224236-mm17-mass-maintenance-of-material-master-in-sap" title="MM17: Massenpflege des Materialstamms in SAP" rel="bookmark">MM17: Massenpflege des Materialstamms in SAP</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="/8226154-20-best-digital-marketing-courses-in-2021" title="20 besten Kurse für digitales Marketing im Jahr 2021" rel="bookmark"><img src="https://cdn.css-code.org/2652133/20_best_digital_marketing_courses_in_2021.jpg.webp" loading="lazy" alt="20 besten Kurse für digitales Marketing im Jahr 2021" title="20 besten Kurse für digitales Marketing 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="/8226154-20-best-digital-marketing-courses-in-2021" title="20 besten Kurse für digitales Marketing im Jahr 2021" rel="bookmark">20 besten Kurse für digitales Marketing im Jahr 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="/8226155-14-best-web-crawler-tools-in-2021" title="14 BEST Web Crawler Tools im Jahr 2021" rel="bookmark"><img src="https://cdn.css-code.org/9779744/14_best_web_crawler_tools_in_2021.png.webp" loading="lazy" alt="14 BEST Web Crawler Tools im Jahr 2021" title="14 BEST Web Crawler Tools 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="/8226155-14-best-web-crawler-tools-in-2021" title="14 BEST Web Crawler Tools im Jahr 2021" rel="bookmark">14 BEST Web Crawler Tools im Jahr 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="/8226156-what-is-software-engineering-definition-basics-characteristics" title="Was ist Software Engineering? Definition, Grundlagen, Eigenschaften" rel="bookmark"><img src="https://cdn.css-code.org/7614756/what_is_software_engineering_definition-_basics-_characteristics.png.webp" loading="lazy" alt="Was ist Software Engineering? Definition, Grundlagen, Eigenschaften" title="Was ist Software Engineering? Definition, Grundlagen, Eigenschaften" 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="/8226156-what-is-software-engineering-definition-basics-characteristics" title="Was ist Software Engineering? Definition, Grundlagen, Eigenschaften" rel="bookmark">Was ist Software Engineering? Definition, Grundlagen, Eigenschaften 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="/8225085-12-best-php-books-2021-update" title="12 BEST PHP Books (Update 2021)" rel="bookmark">12 BEST PHP Books (Update 2021)</a></h3>
									</div>
								</div>
							</li><li class="post-list-small__item">
								<div>
									<div>
										<h3 class="post-list-small__entry-title"><a href="/8225086-php-tutorial-pdf-basic-book-for-beginners-download-now" title="PHP Tutorial PDF: Grundlegendes Buch für Anfänger (JETZT herunterladen)" rel="bookmark">PHP Tutorial PDF: Grundlegendes Buch für Anfänger (JETZT herunterladen)</a></h3>
									</div>
								</div>
							</li><li class="post-list-small__item">
								<div>
									<div>
										<h3 class="post-list-small__entry-title"><a href="/8225087-what-is-plsql-full-form-architecture-of-plsql-developer" title="Was ist PL / SQL? Vollständige Form, Architektur von PL / SQL Developer" rel="bookmark">Was ist PL / SQL? Vollständige Form, Architektur von PL / SQL Developer</a></h3>
									</div>
								</div>
							</li><li class="post-list-small__item">
								<div>
									<div>
										<h3 class="post-list-small__entry-title"><a href="/8225088-sql-vs-plsql-vs-t-sql-key-differences" title="SQL gegen PL / SQL gegen T-SQL: Hauptunterschiede" rel="bookmark">SQL gegen PL / SQL gegen T-SQL: Hauptunterschiede</a></h3>
									</div>
								</div>
							</li><li class="post-list-small__item">
								<div>
									<div>
										<h3 class="post-list-small__entry-title"><a href="/8225089-plsql-first-program-hello-world-example" title="PL / SQL First Program: Hallo Welt Beispiel" rel="bookmark">PL / SQL First Program: Hallo Welt Beispiel</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="/8002991-mask-mode" title="Maskenmodus - CSS-Tricks" rel="bookmark">Maskenmodus - CSS-Tricks</a></h3>
									</div>
								</div>
							</li><li class="post-list-small__item">
								<div>
									<div>
										<h3 class="post-list-small__entry-title"><a href="/8002992-mix-blend-mode" title="Mix-Blend-Modus - CSS-Tricks" rel="bookmark">Mix-Blend-Modus - CSS-Tricks</a></h3>
									</div>
								</div>
							</li><li class="post-list-small__item">
								<div>
									<div>
										<h3 class="post-list-small__entry-title"><a href="/8002993-min-width" title="Min-width - CSS-Tricks" rel="bookmark">Min-width - CSS-Tricks</a></h3>
									</div>
								</div>
							</li><li class="post-list-small__item">
								<div>
									<div>
										<h3 class="post-list-small__entry-title"><a href="/8002994-mask-image" title="Maskenbild - CSS-Tricks" rel="bookmark">Maskenbild - CSS-Tricks</a></h3>
									</div>
								</div>
							</li><li class="post-list-small__item">
								<div>
									<div>
										<h3 class="post-list-small__entry-title"><a href="/8002996-max-inline-size" title="Max-inline-size - CSS-Tricks" rel="bookmark">Max-inline-size - 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="/8226124-90-best-adobe-illustrator-courses-in-2021" title="90 besten Adobe Illustrator-Kurse im Jahr 2021" rel="bookmark">90 besten Adobe Illustrator-Kurse im Jahr 2021</a></h3>
									</div>
								</div>
							</li><li class="post-list-small__item">
								<div>
									<div>
										<h3 class="post-list-small__entry-title"><a href="/8226125-40-best-free-mp4-video-editor-2021" title="40+ Bester KOSTENLOSER MP4-Video-Editor (2021)" rel="bookmark">40+ Bester KOSTENLOSER MP4-Video-Editor (2021)</a></h3>
									</div>
								</div>
							</li><li class="post-list-small__item">
								<div>
									<div>
										<h3 class="post-list-small__entry-title"><a href="/8226126-11-best-free-drawing-courses-and-certification-2021-update" title="11 besten kostenlosen Zeichenkurse & Zertifizierung (2021 Update)" rel="bookmark">11 besten kostenlosen Zeichenkurse & Zertifizierung (2021 Update)</a></h3>
									</div>
								</div>
							</li><li class="post-list-small__item">
								<div>
									<div>
										<h3 class="post-list-small__entry-title"><a href="/8226128-80-best-autocad-courses-in-2021" title="80 besten AutoCAD-Kurse im Jahr 2021" rel="bookmark">80 besten AutoCAD-Kurse im Jahr 2021</a></h3>
									</div>
								</div>
							</li><li class="post-list-small__item">
								<div>
									<div>
										<h3 class="post-list-small__entry-title"><a href="/8226130-what-is-pmp-certification-cost-benefits" title="Was ist PMP? Zertifizierungskosten, Nutzen" rel="bookmark">Was ist PMP? Zertifizierungskosten, Nutzen</a></h3>
									</div>
								</div>
							</li>
						</ul>
					</div>
				</div>
			</div>
			<p>© Copyright de.css-code.org, 2025 August | 
				<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>