开发者

Problem with document.location.href

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 开发者_StackOverflow社区(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


Add a slash in the beginning, ie:

document.location.href = "/en/index.jsp";

Currently, you are redirecting using a relative path when you want to redirect using an absolute path. Slashes in the beginning always means absolute.

If you've ever used a Unix machine, you'd know that /etc/123/abc is a path that goes from the root, whereas etc/123/abc/ would be a relative path, building on the current directory. The same is true here.


If this is a commercial site and you care about your Google ranking then you should be cautious about using JavaScript redirects.

Search engine crawlers cannot follow these kinds of redirects. It would be better to process it on the server side and perform a true 301 redirect.

Also you should give some way to manually change this by clicking a button in your UI.

This code doesn't make any sense to me:

 //Explorer
 if (navigator.userLanguage) 
     language = navigator.userLanguage; 

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

It seems to check if .userLanguage is populated and if it isnt it checks if .language is populated and if that isn't it uses .userLanguage which by this point has already been deemed as undefined.

I would refactor the code something like this:

 if (IsCookieSet()) {
   if (IsCookieLanguage("en-US")) {
       document.location.href = "en/index.jsp";
   }
 }
 else {
   language = navigator.userLanguage ? navigator.userLanguage : navigator.language;

   if (!IsCookieSet()){
       setCookie('language', language, 365, '/', 'onCheck');

     if (IsCookieLanguage("en-US")) {
         document.location.href = "en/index.jsp";
     }
     else if(IsCookieLanguage("sv"))
     {
         document.location.href="index.jsp";      
     }
   }
 }


function IsCookieSet()
{
  return language!=null && language!="";
}

function IsCookieLanguage(lang)
{
  return language.toLowerCase() == lang.toLowerCase();
}

Well that code is a bit cleaner but it still doesn't make much sense because you haven't included all of your code - ie the bit that retrieves the cookie.


It seems you are already on a page in blabla/en/ then. Check that out.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜