子域上的跨域Cookie问题

时间:2019-02-25 07:34:32

标签: javascript cookies

我有三个站点,URL为-

www.xyz.com
account.xyz.com
admin.xyz.com

我希望所有三个共享相同的cookie。如果我打开上述任何一个URL,则应该可以看到我的模式弹出窗口并应该存储cookie,但是如果我尝试打开其他URL链接,则弹出窗口在设置cookie的情况下不可见。

我已经开发了逻辑,并且可以在具有不同端口的localhost中完美运行,但对于实时站点则没有。它仅适用于一个站点!有人可以帮我解决这个问题吗?

谢谢!

代码-

$(window).load(function() {

  $('#slideinModal').modal('show');

  var popVisible = true;

  if (popVisible === true) {
    $('body').css({
      'overflow': 'inherit',
      'padding': '0'
    });

    // Set Cookie
    function setCookie(cname, cvalue, exdays) {
      var now = new Date();
      var time = now.getTime();
      time += 24 * 3600 * 1000;
      now.setTime(time);
      document.cookie = 'username=' + cname + '; expires=' + now.toUTCString() + '; path=/';
    }

    function getCookie(cname) {
      var name = cname + "=";
      var decodedCookie = decodeURIComponent(document.cookie);
      var ca = decodedCookie.split(';');
      for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') {
          c = c.substring(1);
        }
        if (c.indexOf(name) == 0) {
          return c.substring(name.length, c.length);
        }
      }
      return "";
    }

    // Check Cookie
    function checkCookie() {
      var user = getCookie("username");
      if (user != "") {
        dynamiClass();
        //alert("Welcome again " + user);
      } else {
        user = popVisible;
        if (user != "" && user != null) {
          setCookie("username", user, 30);
        }
      }
    }
    checkCookie();
    popVisible = false;
  }
});

// Toggle Logic
$('body').click(function (evt) {
    if ($(evt.target).closest('.modal-backdrop').length) {
        dynamiClass();
    }
});

// Close Modal on Overlay
$('.toggle-offer-modal').on('click', function (e) {
    e.preventDefault();
    dynamiClass();
});

$('.toggleSlide-button').click(function () {
    dynamiClass();
});

function dynamiClass() {
    var oh = 250; //$('#slideinModal .modal-content').outerHeight();
    $('.toggle-offer-modal').parents('#slideinModal').toggleClass('toggleActive');

    if ($('#slideinModal').hasClass('toggleActive')) {
        $('.toggleSlide-button').addClass('open');
        $('.toggle-offer-modal').parents('#slideinModal').css('bottom', -oh + 40);
        $('body').addClass('slideInOn');
        $('body').removeClass('modal-open');
        $('body').attr('style', '');
    }
    else {
        $('.toggleSlide-button').removeClass('open');
        $('.toggle-offer-modal').parents('#slideinModal').css('bottom', 0);
        $('body').removeClass('slideInOn');
        $('body').css({ 'overflow': 'inherit', 'padding': '0' });
    }
}

0 个答案:

没有答案