Closed Thread Icon

Topic awaiting preservation: javascript cookies Pages that link to <a href="https://ozoneasylum.com/backlink?for=25148" title="Pages that link to Topic awaiting preservation: javascript cookies" rel="nofollow" >Topic awaiting preservation: javascript cookies\

 
Author Thread
FatRod
Bipolar (III) Inmate

From: #UK SURREY
Insane since: May 2002

posted posted 03-02-2005 11:36

I need to implement a cookie and need some help - even after some research I cant find a "cut and paste" example that I understand!!!

Basically I have an index page that has 2 choices of industry. I need the choice to be stored in a cookie so that when users return they are always directed to the correct page.

I though of having a remember choice checkbox that when ticked will drop the cookie.

can anyone help me or direct me to a script that is easily customisable?

cheers

FR

poi
Paranoid (IV) Inmate

From: France
Insane since: Jun 2002

posted posted 03-02-2005 12:38

Why not checking the FAQ ?

FatRod
Bipolar (III) Inmate

From: #UK SURREY
Insane since: May 2002

posted posted 03-02-2005 13:16

I have found this code... it uses check boxs to deploy the cookie - is there anyway to use <a href> tags to set the cookie if just one check box is ticked. i.e. 2 links (bs & it) one check box and no form elements to call just <a> tags?


code:
<SCRIPT LANGUAGE="JavaScript">
<!-- Original: Ronnie T. Moore -->
<!-- Web Site: The JavaScript Source -->

<!-- This script and many more are available free online at -->
<!-- The JavaScript Source!! http://javascript.internet.com -->

<!-- Begin
var expDays = 30;
var exp = new Date();
exp.setTime(exp.getTime() + (expDays*24*60*60*1000));

function getCookieVal (offset) {
var endstr = document.cookie.indexOf (";", offset);
if (endstr == -1)
endstr = document.cookie.length;
return unescape(document.cookie.substring(offset, endstr));
}
function GetCookie (name) {
var arg = name + "=";
var alen = arg.length;
var clen = document.cookie.length;
var i = 0;
while (i < clen) {
var j = i + alen;
if (document.cookie.substring(i, j) == arg)
return getCookieVal (j);
i = document.cookie.indexOf(" ", i) + 1;
if (i == 0) break;
}
return null;
}
function SetCookie (name, value) {
var argv = SetCookie.arguments;
var argc = SetCookie.arguments.length;
var expires = (argc > 2) ? argv[2] : null;
var path = (argc > 3) ? argv[3] : null;
var domain = (argc > 4) ? argv[4] : null;
var secure = (argc > 5) ? argv[5] : false;
document.cookie = name + "=" + escape (value) +
((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
((path == null) ? "" : ("; path=" + path)) +
((domain == null) ? "" : ("; domain=" + domain)) +
((secure == true) ? "; secure" : "");
}
function DeleteCookie (name) {
var exp = new Date();
exp.setTime (exp.getTime() - 1);
var cval = GetCookie (name);
document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
}

var favorite = GetCookie('selection');

if (favorite != null) {
switch (favorite) {
case 'it' : url = '/it/index.shtml'; // change these!
break;
case 'bs' : url = '/bs/index.shtml';
break;
}
window.location.href = url;
}
// End -->
</script>
</HEAD>



call is

code:
<form>
<table><tr><td>

<input type=checkbox name="it" onClick="SetCookie('selection', this.name, exp);">it<br>
<input type=checkbox name="bs" onClick="SetCookie('selection', this.name, exp);">bs<br>

</td></tr>
</table>
</form>
</center>



(Edited by FatRod on 03-02-2005 13:19)

poi
Paranoid (IV) Inmate

From: France
Insane since: Jun 2002

posted posted 03-02-2005 14:27

Sorry, without a link I'm too tired to look closely at that (unindented) source code.

Check the How do I set and read cookies in JavaScript?, and you may also have a look at the GreaseMonkey: Pouet "attention whores" toggle user script I did to look my setCookie( ), getCookie( ) functions and how I trigger them.

Hope that helps,



(Edited by poi on 03-02-2005 14:27)

FatRod
Bipolar (III) Inmate

From: #UK SURREY
Insane since: May 2002

posted posted 03-02-2005 14:46

Thanks POI, not that any of it makes any sense to me - i have no javascript skills yet (apart from cut and paste). i have read through the tutorials and either they do not offer what i am looking for or more likely that i just dont understand.

I'll read it all again and see if that helps

kuckus
Paranoid (IV) Mad Librarian

From: Glienicke
Insane since: Dec 2001

posted posted 03-02-2005 21:26

Assuming that the cookie functions you found do their job properly that's perfectly doable.

Say you have this checkbox

code:
<form name="cookie_option">
<input type="checkbox" name="remember_choice" /> text.....
</form>



Then a link that checks whether the cookie needs to be set before the next page is called would look like this:

code:
<a href="page2.html" onclick="if (document.forms['cookie_option'].elements['remember_choice'].checked) 
{ SetCookie('selection', 'it'); }">text....</a>



Hope that helps getting you started
kuckus

[edit: added tiny line break]

(Edited by kuckus on 03-02-2005 21:31)

FatRod
Bipolar (III) Inmate

From: #UK SURREY
Insane since: May 2002

posted posted 03-03-2005 17:24

thanks kuckus,

works a treat! cheers!

how would i call the delete cookie function?

<a href="page.html" onclick="DeleteCookie {'selection'};" > maybe????

cheers

poi
Paranoid (IV) Inmate

From: France
Insane since: Jun 2002

posted posted 03-03-2005 17:35

Call the DeleteCookie( ) function just like you call the SetCookie( ). They are basically the same, and behave the same way.

FatRod
Bipolar (III) Inmate

From: #UK SURREY
Insane since: May 2002

posted posted 03-07-2005 13:19

I cant seem to get the deleteCookie function to work - this is the call i have:

code:
<a href="http://www.domain.co.uk/index.shtml" title="Home" onclick=" {DeleteCookie('selection'); }">home</a>



i have tried a few variations but none seem to work

(Edited by FatRod on 03-07-2005 13:19)

poi
Paranoid (IV) Inmate

From: France
Insane since: Jun 2002

posted posted 03-07-2005 13:26

have you tried :

<a href="http://www.domain.co.uk/index.shtml" title="Home" onclick="void( DeleteCookie('selection') )">home</a>

FatRod
Bipolar (III) Inmate

From: #UK SURREY
Insane since: May 2002

posted posted 03-07-2005 14:53

thanks poi. yep works but only if you restart i.e.... is that right?

poi
Paranoid (IV) Inmate

From: France
Insane since: Jun 2002

posted posted 03-07-2005 15:39

it should work without restarting your "browser". Try in another browser. The cache of IE is well known for being hungry. It might trick you.



(Edited by poi on 03-07-2005 15:40)

FatRod
Bipolar (III) Inmate

From: #UK SURREY
Insane since: May 2002

posted posted 03-08-2005 10:22

actually the cookie gets deleted every time i restart IE, regardless of the whether i call the DeleteCookie function or not... it does not make sense, it is meant to expire after 30 days surely?

poi
Paranoid (IV) Inmate

From: France
Insane since: Jun 2002

posted posted 03-08-2005 11:17

The cookie settings of the browsers are more important than the expiration date. If a user set its browser ( IE or not ) to delete the cookies on close, there's nothing you can do to make the cookies stay on there.

« BackwardsOnwards »

Show Forum Drop Down Menu