记住选择并将用户重定向到访问过的子域

时间:2013-11-27 04:37:02

标签: javascript redirect subdomain

当用户访问example.com时,他会看到许多其他子域(城市)可供选择。我想让用户选择或输入(在地址栏上)a.example.com,在他下次访问example.com时,他应该被重定向到a.example.com。当他访问b.example.com时,他应该被重定向到b.example.com。

我找到了这段代码Drop down menu to remember selection and redirect user,它接近我的需要。我想删除下拉菜单(表单)并替换为与所有城市一起列出的单个页面。我测试了下面的代码,重定向不准确。我需要更健壮,更快速的脚本。任何帮助将非常感谢。

<script type="text/javascript">
if (localStorage && localStorage.country) {
    location = localStorage.country;    
}

function formChanged(form) {
    var val = form.options[form.selectedIndex].value;
    if (val !== 'non-value') {
      if (localStorage) {
        localStorage.country = val;
      }
      location = val;
    }
}
</script>

<FORM NAME="form1">
  <select onchange="formChanged(this);" NAME="country" SIZE="1">
    <OPTION VALUE="non-value">Select Country
    <OPTION VALUE="chile">Chile
    <OPTION VALUE="colombia">Colombia
    <OPTION VALUE="bolivia">Bolivia
  </select>
</FORM>

1 个答案:

答案 0 :(得分:0)

这是您使用Cookie时的情况。

function setSelection(elem){
  var exdate=new Date();
  exdate.setDate(exdate.getDate() + 99999); // A really not time, AKA never
  var c_value=escape(elem.value) + exdate.toUTCString();
  document.cookie="selection=" + c_value;
}

引用此处获取值,然后您可以使用它。正如您将看到的,上面几乎是来自页面的复制粘贴。 http://www.w3schools.com/js/js_cookies.asp

相关问题