Problem with document.location.href

Posted by novellino on Stack Overflow See other posts from Stack Overflow or by novellino
Published on 2010-04-15T08:55:47Z Indexed on 2010/04/15 9:03 UTC
Read the original article Hit count: 308

Filed under:
|
|

Hello, I am new to Javascript and Web development and I have a question regarding the document.location.href. I am using a cookie for storing the language the user prefers and then load the english or the swedish version depending on the language. The default language in the beginning is the same as the browser's language, and my index.jsp is the swedish one. The first time everything works fine. The problem is when the cookie exists already. The basic code is:

    if (language!=null && language!=""){
        if (language=="en-US" || language=="en-us")
       document.location.href = "en/index.jsp";
     }
    else{
 //Explorer
 if (navigator.userLanguage) 
     language = navigator.userLanguage; 

 //other browsers
 else   
     language = (navigator.language) ? navigator.language : navigator.userLanguage;

 if (language!=null && language!=""){
     setCookie('language', language, 365, '/', 'onCheck');

 if (language=="en-US" || language=="en-us")
     document.location.href = "en/index.jsp";

 else if(language=="sv")
     document.location.href="index.jsp";      
      }
    }

When the cookie exists we enter the first "if", and there, if the language is swedish it opens the default blabla/index.jsp page. When the language is set to engish it should open the blabla/en/index.jsp but instead it opens the blabla/en/en/index.jsp which of course is wrong.

Does anyone know what I am doing wrong?? Thanks

© Stack Overflow or respective owner

Related posts about document

Related posts about JavaScript