Jump to content
CDanielOL

Apagar cookie

Recommended Posts

CDanielOL

O que estou a fazer de errado na função para apagar o Cookie?

function busca()
{
	if (document.cookie.length>0)
	{
	  var inicio=document.cookie.indexOf(document.getElementById("nome").value);
	  alert(inicio);
	  if (inicio!=-1)
	  { 
			inicio=document.cookie.indexOf(document.getElementById("nome").value,inicio);
			alert(inicio);
			var fim=document.cookie.indexOf(";",inicio);
			if (fim==-1) fim=document.cookie.length;
			var texto=document.cookie.substring(inicio,fim)
			alert(texto);
			campos=texto.split(";");
			document.getElementById("nome").value=campos[0];

	  } 
	}
}
function registar()
{
	var expire = "";
	var nome=document.getElementById("nome").value;
	expire = new Date((new Date()).getTime() + 365);
	expire = "; expires=" + expire.toGMTString();
	document.cookie = ''+nome+'='+nome+'; expires=Thu, 2 Aug 2100 20:47:11 UTC; path=/';
	alert(document.cookie);
}
function apagar()
{
var cookieI = document.cookie.split("; ");
expire = new Date((new Date()).getTime() - 1);
for (var i = 0; i < cookieI.length; i++) 
{
	var cookieData = cookieI[i].split("=")[0];
	cookieData += "=;expires=" + expire.toGMTString();
	document.cookie = cookieData; 
}
alert(document.cookie)
}

Share this post


Link to post
Share on other sites
cra2sh

function login()
{
var nome = document.getElementById('nome').value;
var expire_date = new Date();
expire_date.setHours(expire_date.getYear()+1);
document.cookie = nome + '=' + nome + '; expires=' + expire_date.toGMTString();
alert('Login efectuado com sucesso. Cookie= ' + document.cookie);
}

function read()
{
var this_cookie = document.cookie.split(';');
for (var i = 0; i < this_cookie.length; i++)

if (this_cookie[i].split('=')[0] == 'nome')
{
	var nome = this_cookie[i].split('=')[1];
	alert('Está logado como ' + nome);
}

else
{
	alert('Não está logado.');
}
}

function logout()
{
var expire_date = new Date();
expire_date.setDate(expire_date.getDate()-1);
document.cookie = 'nome=; expires=' + expire_date.toGMTString();
alert('Logout efectuado com sucesso. Cookie= ' + document.cookie);
}

Não testei, mas espero ter ajudado.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.