最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Problem with document.location.href - Stack Overflow

programmeradmin1浏览0评论

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

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

Share Improve this question edited Apr 15, 2010 at 8:57 Sarfraz 383k82 gold badges559 silver badges612 bronze badges asked Apr 15, 2010 at 8:55 novellinonovellino 111 gold badge1 silver badge2 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 4

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 mercial 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)

  1. 暂无评论