2

I need that when a user opens my website, he detects the language and redirects it to the language of the browser. I'm trying this but it goes into a loop and the page is constantly loaded. I hope you can help.

window.onload = function() {

  var ln = window.navigator.language||navigator.browserLanguage;

  if(ln == 'en'){

    window.location.href = 'index_en.html';

  }else if(ln == 'es'){

    window.location.href = 'index_es.html'; 

  }else{

    window.location.href = 'index_es.html'; 

  }

}
4
  • I assume you'll want to redirect only if the loaded version is different than ln... Commented Nov 25, 2018 at 14:39
  • You could use a cookie to track the assumed language. If it's unset, then do what you're doing but first set the cookie. If it's set, then just see if it's the same as the detected language and if so no reload is necessary. Commented Nov 25, 2018 at 14:40
  • Only put the code in index.html and not in index_en.html etc? Commented Nov 25, 2018 at 15:45
  • 1
    If I speak spanish and manually elect to load the _es page, I wouldn't want it redirecting to _en. Commented Nov 25, 2018 at 15:46

1 Answer 1

6

Set a cookie before the redirect. Then on the next reload the cookie will have a value and you can stop the script using 'return'

window.onload = function() {

    var ln = window.navigator.language||navigator.browserLanguage;
    var myApp = {}

    /**
     * Gets cookie value by name
     * @param  {string} name Name of cookie to retrieve
     * @return {string}      Value of cookie if found
     */
    myApp.ReadCookie = function(name) {
        var nameEQ = name + "=";
        var ca = document.cookie.split(';');
        for(var i=0;i < ca.length;i++) {
            var c = ca[i];
            while (c.charAt(0)==' ') c = c.substring(1,c.length);
            if (c.indexOf(nameEQ) === 0) return c.substring(nameEQ.length,c.length);
        }
        return null;
    };

    /**
    * Removes cookie value
    * @param  {string} name Name of cookie
    */
    myApp.EraseCookie = function(name) {
        if ( myApp.ReadCookie(name) )
        document.cookie = name+'=';
        console.log(name+' erased.');
    };

    /**
    * Deletes cookie reference
    * @param  {string} name Name of cookie
    */
    myApp.DeleteCookie = function(name) {
        document.cookie = name + '=; expires=Thu, 01 Jan 1970 00:00:01 GMT;';
        console.log(name+' deleted.');
    };

    /**
    * Set cookie value
    * @param  {string} name Name of cookie
    */
    myApp.SetCookie = function(name, value, expires) {

        var cookiestring = [[name, '=', encodeURIComponent( value )].join('')];
        var expire_time = '';

        if ( expires ) {
            expire_time = new Date();
            expire_time.setTime( expire_time.getTime() + expires );
            expire_time = expire_time.toGMTString();
            cookiestring.push( ['expires=', expire_time ].join('') );
        }
        cookiestring = cookiestring.join(';')+';';
        document.cookie = cookiestring;
        console.log( 'SetCookie: '+ name +' set to "'+ value +'"', 'Expires?', expire_time );
    };

    if(myApp.ReadCookie('lang_redirect')) {
        return;
    }

    myApp.SetCookie('lang_redirect', ln);

    if(ln == 'en'){
        window.location.href = 'index_en.html';
    }else if(ln == 'es'){
        window.location.href = 'index_es.html'; 
    } else{
        window.location.href = 'index_es.html'; 
    }

}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.