views:

337

answers:

5

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

+2  A: 

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.

Jonatan Littke
A: 

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

npup
+1  A: 

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.

rtpHarry
+1  A: 

Thanks a lot guys! I found my error. The problem is that I called these functions in onLoad(). And I had not understood that the "location" reloads again the page. So probably it did twice the check and this provoked a problem. I delete calling the functions in onLoad an it worked fine!

I have also an option in the UI for changing the language and thus changing the cookie as well. I will try also your code rtpHarry as it looks in dead cleaner!

But you said also: "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." What do you mean with it? How can I do it?

Thanks again a lot!!

novellino
That's actually a separate question but something like this, for example: `<?php Header("Location: URL");?>`. That's a server-side redirect. What you are doing could be called a client-side redirect.
Jonatan Littke
So you were in the `en/' folder as i guessed :) As for the redirect question: The crawlers typically do not have javascript enabled, so they will not see what most users see (and won't index based on that "normal" state of your site). So make the server look for the locale value (which you curently have in a cookie) and direct to the appropriate folder/page based on that. Then the crawlers and ordinary users will have the same view of your site, so to speak.
npup
A: 

Ok I will try also to check the redirection thing for the crawlers. Thanks again!

novellino