使用javascript检测语言并重定向到另一个页面

时间:2018-11-25 14:34:00

标签: javascript html redirect url-redirection

我需要一个用户打开我的网站时,他能够检测到该语言并将其重定向到浏览器的语言。我正在尝试此操作,但是它进入了循环,并且页面不断加载。希望您能提供帮助。

spark-submit \
--class spark.IgniteTester \
--master yarn \
--deploy-mode master \
--driver-memory 1g \
--executor-cores 1 \
--num-executors  1 \
--executor-memory 1664mb \
ignite-tester.jar

1 个答案:

答案 0 :(得分:0)

在重定向之前设置一个cookie。然后在下一次重新加载时,cookie将具有一个值,您可以使用“返回”来停止脚本

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

}