在起始页上重新分配 localStorage

reassign localStorage on start page

我正在使用 localStorage 设置当前语言和当前国家/地区

我需要用 localStorage 设置当前国家,因为在不同的国家列出了相同的语言

类似于 http://abb.com

它工作正常,用于选择当前语言和当前国家 但是对于起始页,我想将语言重新分配给 "Swedish",将国家/地区重新分配给 "Sweden" 现在,如果最后选择的语言是 Hungary - Hungarian,那么当我重新打开网站或导航到起始页时,localStorage 会设置 Hungary - Hungarian 而不是 Swedish Sweden

如果网站的根是 .com,而不是特定语言,我想在重新访问该网站时以某种方式清除 localStorage url

我该怎么做?

目前的代码如下:

jQuery('.ui-tabs-panel .country a').click(function(){
        var selectCountry = jQuery(this).siblings('span').text();
        var selectLanguage = jQuery(this).text();

        if(typeof(Storage) !== "undefined") {
            localStorage.setItem("countryname", selectCountry);
            localStorage.setItem("languagename", selectLanguage);
        } else {
            console.log('Sorry! No Web Storage support..');
        }

    });

    jQuery('.mysite-language').text(currentLangVal);
    jQuery('.mysite-country').text(localStorage.getItem("countryname"));
    jQuery('.mysite-language').text(localStorage.getItem("languagename"));

我试过做类似

的事情

如果 window.location.path 小于一(意味着我们在起始页) 然后设置 localStorage.countryname = "Sweden"localStorage.languagename = "Swedish"

但是没用

我想你几乎已经做到了,只是它不是 window.location.path,而是 window.location.pathname,也不是 "less than one",而是 == '/'。所以:

if(window.location.pathname == '/') {
    // Set language to Swedish
} else {
    // Keep the current language
}