Jump to content

Javascript cookie help


pleek

Recommended Posts

ok so i have this code

 

<html>
<head>
<body>
<SCRIPT TYPE="text/javascript" LANGUAGE="JavaScript">



if (document.cookie = "Show") {
document.write("<b>SHOW</b>");
} 
else {
document.write("<b>HIDE</b>");
}

//SHOUT BOX MINUS & EXPAND
function hide() {
document.getElementById("frame1").height="0";
document.getElementById("BAR").src="http://craigh.tlcrepair.net/mkportal/modules/gallery/album/a_4.gif";
document.getElementById("BAR").onclick=show;
document.getElementById("BARUP").src="http://craigh.tlcrepair.net/mkportal/modules/gallery/album/a_6.gif";
document.cookie = "Hidden";
}

function show() {
document.getElementById("frame1").height="250";
document.getElementById("BAR").src="http://craigh.tlcrepair.net/mkportal/modules/gallery/album/a_3.gif";
document.getElementById("BAR").onclick=hide;
document.getElementById("BARUP").src="http://craigh.tlcrepair.net/mkportal/modules/gallery/album/a_5.gif";
document.cookie = "Show";
}
//END SHOUT BOX MINUS & EXPAND
</script>

<IMG ID="BARUP" SRC="http://craigh.tlcrepair.net/mkportal/modules/gallery/album/a_5.gif" WIDTH="100%" HEIGHT="6">

<!-- SHOUT BOX -->
<IFRAME ID="frame1" SRC="http://www2.shoutmix.com/?pleek" WIDTH="100%" HEIGHT="275" FRAMEBORDER="0" ALIGN="Center" SCROLLING="no">
</IFRAME>
<!-- END SHOUT BOX-->

<IMG ID="BAR" CLASS="pointer" SRC="http://craigh.tlcrepair.net/mkportal/modules/gallery/album/a_3.gif" WIDTH="100%" HEIGHT="15" ONCLICK="hide()">


<P>Use the following buttons to experiment with these statements:</P>
<FORM>

<INPUT TYPE="button" VALUE="display cookie" onClick="alert(document.cookie);">
</FORM>
</head>


</body>
</html>

 

Its supposed to change the words at the top from SHOW to HIDDEN acourding to the cookie. Now i change the cookie by colapsing the shoutbox. I then click show cookie and it says its HIDDEN. But when i click refresh it changes it right back to SHOW and the text at the top doesn't change. CAN anyone figure out what im doing wrong???????????????????

Link to comment
Share on other sites

i have and i don't get it.

 

document.cookie="nameofCookie=cookieData" from other things ive seen i can just use this, but i don't understand on how to use the if statement to check if the name is hidden or show. Can some one explain how to use the cookie to see if the text should be hidden or not?

Link to comment
Share on other sites

like say i have this code

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<HTML>
<HEAD>
<TITLE>Untitled</TITLE>
<META NAME="GENERATOR" CONTENT="MAX's HTML Beauty++ 2004">
</HEAD>

<BODY>
<SCRIPT TYPE="text/javascript" LANGUAGE="JavaScript">
if (document.cookie = "shoutbox=hidden") {
document.write("<B>WOW it works finally</B>");
} 
else {
document.write("<B>YEAH!!!!</B>");
}
document.cookie = "shoutbox=show; expires=15/02/2010 00:00:00";
</SCRIPT>
<INPUT type="button" value="Get Cookie String" name="shoutbox"
onClick='alert(document.cookie)'>
</BODY>
</HTML>

 

What am i doing wrong, ive read all the stuff for like the first 3 sites that came up for "javascript cookies" and none of them help me with this part

Link to comment
Share on other sites

ok I think i got the cookie part FINALLY!!!! Here is my new code, The cookie works in changing the text at the top to tell me which cookie is set but i can't figure out how to get it to run the show() hide() functions depending on the cookie. I tried just copy and pasting in the dom of the functions into my if/else command but that didn't work, can somebody help me out and tell me how to make it so if the cookie says hidden it will run the hide() function?



<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<HTML>
<HEAD>
<TITLE>Untitled</TITLE>
<META NAME="GENERATOR" CONTENT="MAX's HTML Beauty++ 2004">
</HEAD>

<BODY>

<SCRIPT TYPE="text/javascript" LANGUAGE="JavaScript">

if (document.cookie == "shoutbox=show") {
document.write("<p>This is the show cookie</p>");
}
else {
document.write("<p>This is the hidden cookie</p>");
}

function set_cookie() {
document.cookie = "shoutbox=hidden; expires=13/06/2010 00:00:00";

}
function remove_cookie() {
document.cookie = "shoutbox=show; expires=13/06/2010 00:00:00";
}


</SCRIPT>
<INPUT TYPE="button" NAME="Get_Cookie" VALUE="Get Cookie" onClick="alert(document.cookie)">
<INPUT TYPE="button" NAME="Set_Cookie" VALUE="Set Cookie" onClick="set_cookie()">
<INPUT TYPE="button" NAME="Remove_Cookie" VALUE="Remove Cookie" onClick="remove_cookie()">

<SCRIPT TYPE="text/javascript" LANGUAGE="JavaScript">

//SHOUT BOX MINUS & EXPAND
function hide() {
document.getElementById("frame1").height="0";
document.getElementById("BAR").src="http://craigh.tlcrepair.net/mkportal/modules/gallery/album/a_4.gif";
document.getElementById("BAR").onclick=show;
document.getElementById("BARUP").src="http://craigh.tlcrepair.net/mkportal/modules/gallery/album/a_6.gif";
}

function show() {
document.getElementById("frame1").height="250";
document.getElementById("BAR").src="http://craigh.tlcrepair.net/mkportal/modules/gallery/album/a_3.gif";
document.getElementById("BAR").onclick=hide;
document.getElementById("BARUP").src="http://craigh.tlcrepair.net/mkportal/modules/gallery/album/a_5.gif";
}
//END SHOUT BOX MINUS & EXPAND
</script>

<IMG ID="BARUP" SRC="http://craigh.tlcrepair.net/mkportal/modules/gallery/album/a_5.gif" WIDTH="100%" HEIGHT="6">

<!-- SHOUT BOX -->
<IFRAME ID="frame1" SRC="http://www2.shoutmix.com/?pleek" WIDTH="100%" HEIGHT="275" FRAMEBORDER="0" ALIGN="Center" SCROLLING="no">
</IFRAME>
<!-- END SHOUT BOX-->

<IMG ID="BAR" CLASS="pointer" SRC="http://craigh.tlcrepair.net/mkportal/modules/gallery/album/a_3.gif" WIDTH="100%" HEIGHT="15" ONCLICK="hide()">
</BODY>
</HTML>

Link to comment
Share on other sites

does anybody here actually help people? or just say go search it? ive read all the information that showed up on google and the page the post above me said to read. Here is my code WHY WON'T IT WORK?????!?!?!?!?!?

 

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<HTML>
<HEAD>
<TITLE>Untitled</TITLE>
<META NAME="GENERATOR" CONTENT="MAX's HTML Beauty++ 2004">
</HEAD>

<BODY>

<SCRIPT TYPE="text/javascript" LANGUAGE="JavaScript">
function hello() {
document.write("<p>Here is some simple text</p>")
}

if (document.cookie == "shoutbox=show") {
hello()
document.write("<p>This is the show cookie</p>");
}
if (document.cookie == "shoutbox=hidden") {
document.write("<p>This is the hidden cookie</p>");
document.getElementById("frame1").height="0";
}

function hide() {
document.getElementById("frame1").height="0";
}
function set_cookie() {
document.cookie = "shoutbox=hidden; expires=13/06/2010 00:00:00";
}
function remove_cookie() {
document.cookie = "shoutbox=show; expires=13/06/2010 00:00:00";
}


</SCRIPT>
<INPUT TYPE="button" NAME="Get_Cookie" VALUE="Get Cookie" onClick="alert(document.cookie)">
<INPUT TYPE="button" NAME="Set_Cookie" VALUE="Set Cookie" onClick="set_cookie()">
<INPUT TYPE="button" NAME="Remove_Cookie" VALUE="Remove Cookie" onClick="remove_cookie()">
<INPUT TYPE="button" Name="Text" VALUE="Text" onClick="text()">
<INPUT TYPE="button" Name="hide" VALUE="hide" onClick="hide()">
<!-- SHOUT BOX -->
<IFRAME ID="frame1" SRC="http://www2.shoutmix.com/?pleek" WIDTH="100%" HEIGHT="275" FRAMEBORDER="0" ALIGN="Center" SCROLLING="no">
</IFRAME>
<!-- END SHOUT BOX-->

</BODY>
</HTML>

Link to comment
Share on other sites

I don't see you using these functions:

 

function createCookie(name,value,days) {
if (days) {
	var date = new Date();
	date.setTime(date.getTime()+(days*24*60*60*1000));
	var expires = "; expires="+date.toGMTString();
}
else var expires = "";
document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
	var c = ca[i];
	while (c.charAt(0)==' ') c = c.substring(1,c.length);
	if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
}

function eraseCookie(name) {
createCookie(name,"",-1);
}

Link to comment
Share on other sites

so i Have to use those functions? what is wrong with the ones i wrote, they follow the guide that was posted for me to use. And if i use those functions then how do i use a if statement to run the hide() function if the cookie says hidden?

You don't have to use them... but they are known to work, and yours aren't.  So I'd start with working code, get your script working, then rewrite them.

Link to comment
Share on other sites

the quirksmode.org page I posted above says that the date part of the cookie should be in UTC (Greenwich) time in the format created by the Date.toGMTString() method. It should come out looking like the below which is definitely not what you have:

 

document.cookie =
  'ppkcookie2=yet another test; expires=Fri, 3 Aug 2001 20:47:11 UTC; path=/';

 

If quirksmode says the date should be in the format above, I would take that seriously.  Who knows javascript, quirksmode knows javascript. Just go ahead and use the functions posted on quirksmode and by fenway to set and test your cookies.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

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.