admin管理员组

文章数量:1424409

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'; 

  }

}

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'; 

  }

}
Share Improve this question edited Nov 25, 2018 at 14:34 Pointy 414k62 gold badges595 silver badges629 bronze badges asked Nov 25, 2018 at 14:34 LucianosainzLucianosainz 231 silver badge5 bronze badges 4
  • I assume you'll want to redirect only if the loaded version is different than ln... – haim770 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. – Pointy Commented Nov 25, 2018 at 14:40
  • Only put the code in index.html and not in index_en.html etc? – fdomn-m 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. – fdomn-m Commented Nov 25, 2018 at 15:46
Add a ment  | 

1 Answer 1

Reset to default 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'; 
    }

}

本文标签: htmldetect language with javascript and redirect to another pageStack Overflow