1

Can someone tell me why this code is not working. I have and index page where you choose your language using 2 links which lead you to language index pages.

I've added this code to my index page:

<script type="text/javascript">
  function get_cookie (username)
  {
    var results = document.cookie.match(username + '=(.*?)(;|$)');

    if (results[1] == "en")
      location.replace("http://x.com");
    if (results[1] == "es")
      location.replace("http://y.com");    
    else
      return null;
  }

  get_cookie('username');
</script>

And this code to my language index pages:

<script type="text/javascript">
  var cookieDate = new Date(2012, 8, 08)
  document.cookie = "username=en;expires=" + cookieDate.toGMTString();
</script>

The weird thing is that when I open index page on my desktop it redirects me, but when I open it on web it doesn't.

Sir Crispalot
  • 4,792
  • 1
  • 39
  • 64
  • Is the cookie apparent on the web page? If it isn't, `match` will return `null` and `results[1]` will fail. Try logging `results`. – pimvdb Jul 21 '12 at 12:32
  • This SO question might be useful to you: [Remember preferable language](http://stackoverflow.com/questions/11383839/remember-preferable-language/11384363#11384363) – Thomas C. G. de Vilhena Jul 21 '12 at 12:37
  • Did you have a code that initiates the "language choice", i.e. creating the cookie? Add a check for `results==null` before using `results[1]` – Alvin Wong Jul 21 '12 at 12:38

0 Answers0