M
matt
I'm a bit stuck with this redirect and javascript in general.
I am building a site which on the first page users are given a list of
cities.
They choose their city, a pop up asks them if they want their browser
to remember their choice for future visits, then they get redirected
to a page which is relevant to their city.
If they opted for their choice to be remembered (ie/ a cookie to be
set) then they would never see the list of cities again (or until the
cookie expires) and instead they would be redirected seamlessly from
the main page to their city page.
Here's what I have so far (only using 2 cities - Bath and
Birmingham... but 50 to be added!):
1. Within <head> and </head>:
<script language="Javascript">
function expireGMT (days)
{
var now=new Date();
now.setTime (now.getTime()+days*24*3600000);
return "; expires=" + now.toGMTString()
}
function select(cc)
{
var city;
if(cc=="Bath")
{
city = "Bath";
}
if(cc=="Birmingham")
{
city = "Birmingham";
}
var answer = confirm("You just selected "+city+"\n\nWould you like
your browser to remember this selection for future visits?");
if(answer)
{
var cookievalue = 'cookiename='+cc+ expireGMT(90)
document.cookie = cookievalue;
}
if(cc=="Bath")
{
document.location="http://www.mydomain.co.uk/bath.htm";
}
if(cc=="Birmingham")
{
document.location="http://www.mydomain.co.uk/birmingham.htm";
}
}
</script>
Inside the body tags I have 2 text links:
<td valign="middle"><span class="style6"><a
href="javascript:select('Bath');">Bath</a></span></td>
<td valign="middle"><span class="style6"><a
href="javascript:select('Birmingham');">Bath</a></span></td>
My problem:
Clicking on one of these 2 cities does create a cookie called
'cookiename' with value 'Bath' or 'Birmingham' and even overwrites if
more visits are done and different options chosen.
Also on clicking the city name the person is taken to that destination
page. However, on revisiting the site, I am faced with the list of
cities again. So it isn't doing the job of being able to read the
cookie and apply a redirect based on that.
Can anyone point me in the direction of where I'm going wrong?
The code that I have gathered and amended leaves me with "cc", not too
sure what this is or if it is just a made up function name that
someone put in there.
Either way I'm really stuck with this and have been searching for
options.
Would these to bits of code eventually allow me to do redirects for
50+ cities or would I need a solution where another file is referenced
eg/ citychoice.js ?
Any help very much appreciated with this,
Thanks
I am building a site which on the first page users are given a list of
cities.
They choose their city, a pop up asks them if they want their browser
to remember their choice for future visits, then they get redirected
to a page which is relevant to their city.
If they opted for their choice to be remembered (ie/ a cookie to be
set) then they would never see the list of cities again (or until the
cookie expires) and instead they would be redirected seamlessly from
the main page to their city page.
Here's what I have so far (only using 2 cities - Bath and
Birmingham... but 50 to be added!):
1. Within <head> and </head>:
<script language="Javascript">
function expireGMT (days)
{
var now=new Date();
now.setTime (now.getTime()+days*24*3600000);
return "; expires=" + now.toGMTString()
}
function select(cc)
{
var city;
if(cc=="Bath")
{
city = "Bath";
}
if(cc=="Birmingham")
{
city = "Birmingham";
}
var answer = confirm("You just selected "+city+"\n\nWould you like
your browser to remember this selection for future visits?");
if(answer)
{
var cookievalue = 'cookiename='+cc+ expireGMT(90)
document.cookie = cookievalue;
}
if(cc=="Bath")
{
document.location="http://www.mydomain.co.uk/bath.htm";
}
if(cc=="Birmingham")
{
document.location="http://www.mydomain.co.uk/birmingham.htm";
}
}
</script>
Inside the body tags I have 2 text links:
<td valign="middle"><span class="style6"><a
href="javascript:select('Bath');">Bath</a></span></td>
<td valign="middle"><span class="style6"><a
href="javascript:select('Birmingham');">Bath</a></span></td>
My problem:
Clicking on one of these 2 cities does create a cookie called
'cookiename' with value 'Bath' or 'Birmingham' and even overwrites if
more visits are done and different options chosen.
Also on clicking the city name the person is taken to that destination
page. However, on revisiting the site, I am faced with the list of
cities again. So it isn't doing the job of being able to read the
cookie and apply a redirect based on that.
Can anyone point me in the direction of where I'm going wrong?
The code that I have gathered and amended leaves me with "cc", not too
sure what this is or if it is just a made up function name that
someone put in there.
Either way I'm really stuck with this and have been searching for
options.
Would these to bits of code eventually allow me to do redirects for
50+ cities or would I need a solution where another file is referenced
eg/ citychoice.js ?
Any help very much appreciated with this,
Thanks