I'm very new to Javascript and I'm trying to create some cookies. I do not seem to be able to save them under document.cookie object for some reason. The main goal is to have a user write something in an text field and upon clicking a button it saves that input in a cookie and in js use document.getElementById("elementID").value; to get the value of the user input. I've tried to create a cookie manually like document.cookie = "name=samu"; but it also does not show. After googling this exact task I found the following page which had no problem creating a cookie from a user input in a text field. When checking under the developer tools > Application > Cookies I could see the cookie appear but I've even copy pasted the code and it did not work. I followed the W3school example on how to set and create one but also did not work.
HTML
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<link rel="stylesheet" href="Stylesheet1.css">
<script type="text/javascript" src="objectosPredef.js"></script>
<title>Actividad 2</title>
</head>
<body onload="listaPropiedades()">
<div id="listaPropiedades"></div>
<input type="text" id="nombre" placeholder="entra tu nombre" />
<button onclick="crearCookie();">Click</button>
</body>
</html>
Javascript
function setCookie(nombre, valor, exp) {
var d = new Date();
d.setTime(d.getTime() + exp * 24 * 60 * 60 * 1000);
var exp = "expires=" + d.toUTCString();
document.cookie = nombre + "=" + valor + ";" + exp + ";path=/";
alert(document.cookie);
}
function crearCookie() {
var nombre = "nombre_usuario";
var valor = document.getElementById("nombre").value;
alert(valor);
var exp = 500;
setCookie(nombre, valor, exp);
}
I'm using Microsoft Visual Studio Community 2019
Thanks in advance!
@mplungjan was right. I've downloaded Xampp and was able to save cookies and see them via the browser! thanks