admin管理员组

文章数量:1335436

The cookie is not holding and the domain example cannot redirect to a.example when I type on the address bar. Any help will be very much appreciated.

$(function() {
  var city = getCookie('city');
  if (city != null && city != '') {
    window.location.href = 'http://' + city + '.example';
  }
  $('#citygo').change(function() {
    var city = $(this).val();
    window.location.href = 'http://' + city + '.example';
  });
});
<select id="citygo">
  <option value="0">Select City</option>
  <option value="amsterdam">Amsterdam</option>
  <option value="newyork">New York</option>
  <option value="london">London</option>
  <option value="cardiff">Cardiff</option>
</select>

Is it like this? It is not working. Here is the code:

<script type="text/javascript" src="/static/js/jquery-1.3.2.min.js"></script>
<script type="text/javascript">
  var cookieName = 'city';
  var cookieValue = 'city';
  var myDate = new Date();
  myDate.setMonth(myDate.getMonth() + 12);
  document.cookie = cookieName + "=" + cookieValue + `";domain=.example;path=/;expires=" + myDate;
</script>

<script type="text/javascript">
  //<![CDATA[
  $(function() {
    var city = getCookie('city');
    if (city != null && city != '') {
      window.location.href = 'http://' + city + '.example';
    }
    $('#citygo').change(function() {
      var city = $(this).val();
      window.location.href = 'http://' + city + '.example';
    });
  });


  function setCookie(name, value, days) {
    if (days) {
      var date = new Date();
      date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
      var expires = "; expires=" + date.toGMTString();
    } else var expires = "";
    document.cookie = name + "=" + value + expires + "; path=/";
  }

  function getCookie(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;
  }

  function dropCookie(name) {
    createCookie(name, "", -1);
  }
  //]]>
</script>

The cookie is not holding and the domain example. cannot redirect to a.example. when I type on the address bar. Any help will be very much appreciated.

$(function() {
  var city = getCookie('city');
  if (city != null && city != '') {
    window.location.href = 'http://' + city + '.example.';
  }
  $('#citygo').change(function() {
    var city = $(this).val();
    window.location.href = 'http://' + city + '.example.';
  });
});
<select id="citygo">
  <option value="0">Select City</option>
  <option value="amsterdam">Amsterdam</option>
  <option value="newyork">New York</option>
  <option value="london">London</option>
  <option value="cardiff">Cardiff</option>
</select>

Is it like this? It is not working. Here is the code:

<script type="text/javascript" src="/static/js/jquery-1.3.2.min.js"></script>
<script type="text/javascript">
  var cookieName = 'city';
  var cookieValue = 'city';
  var myDate = new Date();
  myDate.setMonth(myDate.getMonth() + 12);
  document.cookie = cookieName + "=" + cookieValue + `";domain=.example.;path=/;expires=" + myDate;
</script>

<script type="text/javascript">
  //<![CDATA[
  $(function() {
    var city = getCookie('city');
    if (city != null && city != '') {
      window.location.href = 'http://' + city + '.example.';
    }
    $('#citygo').change(function() {
      var city = $(this).val();
      window.location.href = 'http://' + city + '.example.';
    });
  });


  function setCookie(name, value, days) {
    if (days) {
      var date = new Date();
      date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
      var expires = "; expires=" + date.toGMTString();
    } else var expires = "";
    document.cookie = name + "=" + value + expires + "; path=/";
  }

  function getCookie(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;
  }

  function dropCookie(name) {
    createCookie(name, "", -1);
  }
  //]]>
</script>
Share Improve this question edited Dec 6, 2023 at 16:19 Heretic Monkey 12.1k7 gold badges61 silver badges131 bronze badges asked Dec 3, 2013 at 5:22 user3040048user3040048 951 gold badge1 silver badge9 bronze badges 4
  • What is this getCookie function? Have you tried alerting or writing the city variable to console.log? – Johnny Harlamert Commented Dec 3, 2013 at 5:28
  • My goal is to set cookie then redirect. Not sure about the console.log. Can you add the simple code? Thanks. – user3040048 Commented Dec 3, 2013 at 5:41
  • I just add some more code so please check the top. Thanks. – user3040048 Commented Dec 3, 2013 at 8:02
  • Please, format your code with proper indentation. Flat, unindented code is much more difficult to read. There is a Tidy button in the Stack Snippets edit that can help. – Heretic Monkey Commented Dec 6, 2023 at 16:21
Add a ment  | 

2 Answers 2

Reset to default 0

By default, cookies are set to only send on the same domain name that they came from. Be sure you set the cookie domain to .example. to share across all subdomains (assuming that you indeed are trying to share across subdomains). /

 $.cookie('city', 'whatevercityhere', { expires: 7, path: '/', domain: '.example.' });

Edit: Per ments below, also check that the cookie isn't set to http-only.

Try this example:

    <script type="text/javascript">
    var cookieName = 'HelloWorld';
    var cookieValue = 'HelloWorld';
    var myDate = new Date();
    myDate.setMonth(myDate.getMonth() + 12);
    document.cookie = cookieName +"=" + cookieValue + ";expires=" + myDate 
                      + ";domain=.example.;path=/";
    </script>

本文标签: javascriptSet cookie then redirectStack Overflow