document.cookie可以安全地设置页面样式吗?

时间:2017-10-22 14:24:42

标签: jquery cookies

我使用cookie来设置用户选择的页面导航器的样式。 我使用带有localStorage的cookie在子域上共享cookie。因为localStorage不能在域之间使用。

我的代码是安全的还是您有任何优化建议?

我的代码:

<script>
$(document).ready(function(){

function getCookie(cname) {
    var name = cname + "=";
    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);
        }
        if (c.indexOf(name) == 0) {
            return c.substring(name.length, c.length);
        }
    }
    return "";
}

 var getUserBarPosition;
  if (localStorage.getItem('UserBarPosition') !== null || getCookie('UserBarPosition') !== null) {
    getUserBarPositionCookie = getCookie('UserBarPosition');
    getUserBarPosition = localStorage.UserBarPosition;
    $('body .modami_menu').addClass(getUserBarPositionCookie);
    $('body .modami_menu').addClass(getUserBarPosition);  }


  if (getUserBarPosition == 'user_header_unfix' || getUserBarPositionCookie == 'user_header_unfix') {
  $('body .modami_text-12').addClass('widgetforUnfixedbar');
  $('body #text-13').addClass('widgetforUnfixedbar');
  $('body .userBarPosition').addClass('fa-unlock-alt');
  }

  $('.Mlockicon').on('click', function() {
    if (getUserBarPosition == 'user_header_unfix' || getUserBarPositionCookie == 'user_header_unfix' ) {
      getUserBarPosition = 'user_header_fix';
      getUserBarPositionCookie = 'user_header_fix';
      $('body .modami_menu').addClass('user_header_fix');

$('body .modami_text-12').removeClass('widgetforUnfixedbar');
  $('body #text-13').removeClass('widgetforUnfixedbar');


      $('body .modami_menu').removeClass('user_header_unfix');
      localStorage.setItem('UserBarPosition', 'user_header_fix');
      document.cookie = "UserBarPosition=user_header_fix; domain=.mydomain.com; path=/";

$('body .userBarPosition').removeClass('fa-unlock-alt');

    } else {
      getUserBarPosition = 'user_header_unfix';
      getUserBarPositionCookie = 'user_header_unfix';
      $('body .modami_menu').addClass('user_header_unfix');
$('body .userBarPosition').addClass('fa-unlock-alt');
      $('body .modami_menu').removeClass('user_header_fix');


$('body .modami_text-12').addClass('widgetforUnfixedbar');
  $('body #text-13').addClass('widgetforUnfixedbar');


      localStorage.setItem('UserBarPosition', 'user_header_unfix');
      document.cookie = "UserBarPosition=user_header_unfix; domain=.mydomain.com;  path=/";
    }
  });

});
</script>

0 个答案:

没有答案