SitePoint Sponsor |
|
User Tag List
Results 1 to 8 of 8
-
Jun 14, 2006, 04:12 #1
- Join Date
- May 2005
- Posts
- 39
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
HELP! Cookie to remember and redirect on the click of a button
Hi all,
I'm not a javascript expert, please help me.
I need a script that will do:
1. The index will have 2 options, option 1 takes you to Page1, option 2 to Page2
2. When the user will visit the page again it will redirect them to the page of their previous option.
I've been looking for hours and I can't find what I need.
Found the following script which is almost perfect
http://javascript.internet.com/cooki...-redirect.html
But when you make your selection you have to reload the page in order to work. I don't think that this is very good.
Is there a way to alter the code and add a button instead of having to refresh the page?
please please help
many thanks
-
Jun 14, 2006, 08:52 #2
- Join Date
- Jun 2006
- Posts
- 3
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I'm not a javascript expert, but a page redirect can be used as follows . . .
<meta HTTP-EQUIV="REFRESH" content="0; url=http://www.gohere.com/index.html">
-
Jun 14, 2006, 08:57 #3
- Join Date
- Jul 2005
- Location
- West Springfield, Massachusetts
- Posts
- 17,290
- Mentioned
- 198 Post(s)
- Tagged
- 3 Thread(s)
optional page load
I think the page has to load again, no?
On initial load - if cookie ? use option : show set cookie button
How would the page load the first time based on a cookie that hasn't been set yet?
Maybe you could do someting with AJAX that would do what you're looking for?Big Change Coming Soon - if you want your PMs save them now!
What you need to do to prepare for our migration to Discourse
A New SitePoint Forum Experience: Our Move to Discourse
-
Jun 14, 2006, 09:51 #4
- Join Date
- May 2005
- Posts
- 39
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Thanks for your reply
but you either didn't read my message or I didn't explain my self correctly.
I will explain again...
Index page with 2 options (Page1, Page2) with check or radio buttons and a 'Go' button.
The user must choose between the two options in order to continue the navigation.
Lets say he chose option 2(Page2) and pressed the 'Go' button...Fantastic
The next day the same user after he will type the url, the index page must remember his choice (with the cookie) and redirect him to Page2 which was his choice.
I hope it's more clear now.
Thank for taking the time to read my message.
-
Jun 14, 2006, 09:57 #5
- Join Date
- May 2005
- Posts
- 39
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
My previous reply was for Clockwork.
Mittineague thanks for your reply but I really don't know what you are talking about.
I don't know javascript I only copy and paste...I've tried though many times
-
Jun 14, 2006, 10:02 #6
- Join Date
- Jul 2005
- Location
- West Springfield, Massachusetts
- Posts
- 17,290
- Mentioned
- 198 Post(s)
- Tagged
- 3 Thread(s)
cookie redirect
It looks like the script sets a 30 day cookie. Do you have your browser set to delete cookies when the browser closes?
Big Change Coming Soon - if you want your PMs save them now!
What you need to do to prepare for our migration to Discourse
A New SitePoint Forum Experience: Our Move to Discourse
-
Jun 15, 2006, 01:08 #7
- Join Date
- May 2005
- Posts
- 39
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
No my browser doesn't delete cookies.
The script works fine I just want instead of hitting the refresh browsers button to have a button to click on.
-
Jun 15, 2006, 01:58 #8
- Join Date
- May 2005
- Posts
- 39
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
thank you all for reading my post and those who replied.
got what i wanted from the http://www.webdeveloper.com forum.
Many thanks to vwphillips who gave me the code
I have a smile on my face again.
I paste the code bellow in case anyone else wants to use it.
---------------------------------------------------------------------------------
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title></title>
<script language="JavaScript" type="text/javascript">
<!--
function Link(){
if (document.fred.r1[0].checked){
window.top.location='http://www.vicsjavascripts.org.uk/';
}
if (document.fred.r1[1].checked){
window.top.location='http://www.js-examples.com/';
}
}
//-->
</script>
</head>
<body onload="f19_GetFormCookie();Link();">
<form title="f19_Include" name="fred" >
<INPUT type="radio" name="r1" >
<INPUT type="radio" name="r1" >
<INPUT type="button" value="GO" onclick="f19_SetFormCookie();Link();" >
</form>
<script language="JavaScript" type="text/javascript">
<!--
// Form Compendium f19_Part1 (12-05-2005)
// Form Cookie
// by Vic Phillips http://www.vicsJavaScripts.org.uk
// A Cookie Script to Store and Retrieve
// the values and states of common form elements
// TEXT BOXES - value
// TEXT AREA - value
// CHECK BOX - checked state
// RADIO BUTTON - checked state
// SELECT LIST - selected Index
// Application Notes and Customising Variables
// Application Notes
// Values and states of each element are stored
// as the page is unloaded or form submitted.
// The storage duration is specified by a customising variable
// Stored values and states of elements included in the cookie
// are re-established as the page is reloaded.
// The number and type of elements included must respect
// the maximum cookie size of 4K.
// The Retrieval is initialised by a <BODY> onload event
// The Storage occurs on a <BODY> onunload event
// e.g.
// <body onload="f19_GetFormCookie();" onunload="f19_SetFormCookie();" >
// To include a form element in the Store and Retrieve
// it must be child nodes of an element with a title of 'f19_Include'
// e.g.
// <span title="f19_Include" >
// <INPUT type="text" size="10" >
// <INPUT type="checkbox >
// </span>
// There may be any number of elements titled 'f19_Include' on a page
// and as many child elements of each 'f19_Include' as required.
// All variable, function etc. names are prefixed with 'f19_'
// to minimise conflicts with other JavaScripts
// Customising Variables
var f19_Days=1; // The cookie will be available on revisits for a specified number of days
var f19_Cookie='My Form2'; // The Cookie name
//-->
</script>
<script language="JavaScript" type="text/javascript">
<!--
// Form Compendium f19_Part2 (12-05-2005)
// Form Cookie
// by Vic Phillips http://www.vicsJavaScripts.org.uk
// Functional Code
// No Need To Change ***************************
var f19_TBAry=new Array();
var f19_RCAry=new Array();
var f19_TAAry=new Array();
var f19_SLAry=new Array();
var f19_TBString,f19_RCString,f19_TAString,f19_SLString;
var f19_,f19_exp,f19_st,f19_len,f19_end,f19_st;
var f19_Exp=new Date(new Date().getTime()+f19_Days*86400000).toGMTString();
function f19_GetFormCookie(){
f19_TBString=f19_GetCookie(f19_Cookie+'TB');
f19_RCString=f19_GetCookie(f19_Cookie+'RC');
f19_SLString=f19_GetCookie(f19_Cookie+'SL');
f19_TAString=f19_GetCookie(f19_Cookie+'TA');
f19_=document.getElementsByTagName('*');
for (f19_0=0;f19_0<f19_.length;f19_0++){
if (f19_[f19_0].title=='f19_Include'){
f19_Inc=f19_[f19_0].getElementsByTagName('*');
for (f19_1=0;f19_1<f19_Inc.length;f19_1++){
if (f19_Inc[f19_1].tagName=='INPUT'){
if (f19_Inc[f19_1].type=='text'){
f19_TBAry[f19_TBAry.length]=f19_Inc[f19_1];
}
if (f19_Inc[f19_1].type=='radio'||f19_Inc[f19_1].type=='checkbox'){
f19_RCAry[f19_RCAry.length]=f19_Inc[f19_1];
}
}
if (f19_Inc[f19_1].tagName=='TEXTAREA'){
f19_TAAry[f19_TAAry.length]=f19_Inc[f19_1];
}
if (f19_Inc[f19_1].tagName=='SELECT'){
f19_SLAry[f19_SLAry.length]=f19_Inc[f19_1];
}
}
}
}
if (f19_TBString){
for (f19_1=0;f19_1<f19_TBAry.length;f19_1++){
f19_TBAry[f19_1].value=f19_TBString.split('~^~')[f19_1];
}
}
if (f19_RCString){
for (f19_2=0;f19_2<f19_RCAry.length;f19_2++){
f19_RCAry[f19_2].checked=false;
if (f19_RCString.split('~^~')[f19_2]=='true'){
f19_RCAry[f19_2].checked=true;
}
}
}
if (f19_TAString){
for (f19_3=0;f19_3<f19_TAAry.length;f19_3++){
f19_TAAry[f19_3].value=f19_TAString.split('~^~')[f19_3];
}
}
if (f19_SLString){
for (f19_4=0;f19_4<f19_SLAry.length;f19_4++){
f19_SLAry[f19_4].selectedIndex=f19_SLString.split('~^~')[f19_4];
}
}
}
function f19_GetCookie(name) {
var f19_st=document.cookie.indexOf(name+"=");
var f19_len=f19_st+name.length+1;
if ((!f19_st)&&(name != document.cookie.substring(0,name.length))) return null;
if (f19_st==-1) return null;
var f19_end=document.cookie.indexOf(";",f19_len);
if (f19_end==-1) f19_end=document.cookie.length;
return decodeURI(document.cookie.substring(f19_len,f19_end));
}
function f19_SetFormCookie(value){
f19_TBString='';
for (f19_0=0;f19_0<f19_TBAry.length;f19_0++){
f19_TBString+=f19_TBAry[f19_0].value+'~^~';
}
document.cookie=f19_Cookie+"TB="+encodeURI(f19_TBString)+";expires="+f19_Exp+";path=/;"
f19_RCString='';
for (f19_1=0;f19_1<f19_RCAry.length;f19_1++){
f19_RCString+=f19_RCAry[f19_1].checked+'~^~';
}
document.cookie=f19_Cookie+"RC="+encodeURI(f19_RCString)+";expires="+f19_Exp+";path=/;"
f19_TAString='';
for (f19_0=0;f19_0<f19_TAAry.length;f19_0++){
f19_TAString+=f19_TAAry[f19_0].value+'~^~';
}
document.cookie=f19_Cookie+"TA="+encodeURI(f19_TAString)+";expires="+f19_Exp+";path=/;"
f19_SLString='';
for (f19_1=0;f19_1<f19_SLAry.length;f19_1++){
f19_SLString+=f19_SLAry[f19_1].selectedIndex+'~^~';
}
document.cookie=f19_Cookie+"SL="+encodeURI(f19_SLString)+";expires="+f19_Exp+";path=/;"
}
//-->
</script>
</body>
</html>
__________________
Vic
http://www.vicsjavascripts.org.uk
remove any spaces between java & script
Bookmarks