Forum Moderators: open

Message Too Old, No Replies

Redirect cookie

Request for javascript code for redirection based on previous choice

         

flaufs

2:50 pm on Aug 14, 2004 (gmt 0)

10+ Year Member


I wonder if anyone could or would help me out? I am a beginner at javascript and what I would like to do is beyond my very limited capabilities (at the moment - I'm studying!)

I am doing a multilanguage site which has an index page (Call it A) where visitors can choose their preferred language - on clicking one of four flags they are taken to the appropriate welcome page (B, C, D or E). I would like to set a cookie so that when they return to A they are automatically redirected to B, C, D or E - whichever was their preferred language.

This means that I need to read and act on the cookie on A, and set the cookie automatically when they load B, C, D or E. (I did think about setting the cookie when they click the flag which sends them to B, C, D or E, but those flags are part of a flash animation, and I don't even know if I can set a cookie using FlashAction scipt. It seems to be easier to set it onload of the new page.)

Trouble is I don't know how to do any of this.

Can anyone help?

Gul_Dukat

7:47 pm on Aug 15, 2004 (gmt 0)

10+ Year Member



I can,

Paste those lines into your pages B, C, D etc.


<script language="JavaScript" type="text/javascript">
var d = new Date()
var dMonth = d.getTime() + 30*24*60*60*1000
d.setTime(dMonth)
document.cookie = 'startPage=' + location.href + '; expires=' + d.toGMTString()
function deleteCookie(){
var d = new Date(2000,1,1)
document.cookie = 'startPage=' + location.href + '; expires=' + d.toGMTString()
}
</script>

And in your page A paste those


<script language="JavaScript" type="text/javascript">
var c = document.cookie.split(';')
if(c.length >0){
for(m=0;m<c.length;m++){
if(c[m].indexOf('startPage')>-1){
location.replace(c[m].split('=')[1])
}
}
}
</script>

But so the user will not longer can load the page A (the cookie will always redirect to other page)

To void this, you can call to javascript function since your flag into your flash movie.

Now, your flag has an action like getURL('the url')

I suggest to add this line to your flag action

getURL('javascript:void deleteCookie()')
getURL('the url')

this line will call the javascript function wich delete the cookie at the moment to click de flag wich loads the page with the default language

greetings

flaufs

12:19 pm on Aug 16, 2004 (gmt 0)

10+ Year Member


Thank you very much for your very detailed help. I did as you suggested, but it is not working so I think I must have missed something. I have redone it a few times and still can't get it to work.

(I am still not when the cookie is being set)

Should I email you the URL so you can see what I am doing wrong?

Thanks in advance for any help you give.

Lord Majestic

12:30 pm on Aug 16, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



JavaScript redirection may not be understood by non-browsers such as web crawlers. At the very least I'd link flags to relevant pages using <a href>'s.

Gul_Dukat

1:20 pm on Aug 16, 2004 (gmt 0)

10+ Year Member



Should I email you the URL so you can see what I am doing wrong?

Sure, can you do it since my profile?

Gul_Dukat

5:22 pm on Aug 16, 2004 (gmt 0)

10+ Year Member



Hi again,

flaufs, I've seen your page and the problem seems to be that there's a sentence wich should be in one line and it's in two

This line

var d = new Date()
var dMonth = d.getTime() + 30*24*60*60*1000
d.setTime(dMonth)
document.cookie = 'startPage=' + location.href + '; expires=' + d.toGMTString()
function deleteCookie(){
var d = new Date(2000,1,1)
document.cookie = 'startPage=' + location.href + '; expires=' + d.toGMTString()
}

Anyway, I see that it is not necessary to use de deleteCookie function, so you can delete it, if you want, and it is not necessary neither the action I mentioned to the flag button into your flash.

So the things should see like this:

In your default page


<script language="JavaScript" type="text/javascript">
var c = document.cookie.split(';')
if(c.length >0){
for(m=0;m<c.length;m++){
if(c[m].indexOf('startPage')>-1){
location.replace(c[m].split('=')[1])
}
}
}
</script>

And in the wellcome pages in every language


<script language="JavaScript" type="text/javascript">
var d = new Date()
var dMonth = d.getTime() + 30*24*60*60*1000
d.setTime(dMonth)
document.cookie = 'startPage=' + location.href + '; expires=' + d.toGMTString()
</script>

Try now and tell me ;-)

flaufs

3:48 pm on Aug 17, 2004 (gmt 0)

10+ Year Member



Hello again,

Still having problems. (sorry for being so thick) The cookie appears to be set when I leave the language page chosen. (However, I'm not sure its being set correctly, as it doesn't show as "cookie:webpage", it simply shows as "languagechosen/" - perhaps this does not matter?)

Anyway, when I go back to the default page I don't get redirected despite the fact that I am storing a cookie. Also, if I choose a different language this time the cookie is not reset.

I really appreciate your time on this and once again, thanks to everyone who has commented.