JS Using cookie to Set Styles

  • 2021-07-01 06:17:52
  • OfStack

This article illustrates how JS uses cookie to set styles. Share it for your reference, as follows:


var styleShow = ["blackgreen", "purple"];
var path = "/";
var StyleSwitch = {
  // Setting Styles 
  setStyleSheet: function (StyleName) {
    var i, a, main;
    for (i = 0; (a = document.getElementsByTagName("link")[i]); i++) {
      if (a.getAttribute("rel").indexOf("Style") != -1 && a.getAttribute("title")) {
        a.disabled = true;
        if (a.getAttribute("title") == StyleName) {
          a.disabled = false;
          this.setCookie("Style", StyleName);
        }
      }
    }
  },
  // Get the currently used style 
  getStyleSheet: function () {
    var i, a;
    for (i = 0; (a = document.getElementsByTagName("link")[i]); i++) {
      if (a.getAttribute("rel").indexOf("Style") != -1 && a.getAttribute("title") && !a.disabled) {
        return a.getAttribute("title");
      }
    }
    return null;
  },
  // Get the default style 
  getPreferredStyleSheet: function () {
    var i, a;
    for (i = 0; (a = document.getElementsByTagName("link")[i]); i++) {
      if (a.getAttribute("rel").indexOf("Style") != -1
    && a.getAttribute("rel").indexOf("alt") == -1
    && a.getAttribute("title")
    ) {
        return a.getAttribute("title");
      }
    }
    return null;
  },
  // Get cookie
  getCookie: function (name) {
    var cookieName = encodeURIComponent(name) + "=",
    cookieStart = document.cookie.indexOf(cookieName),
    cookieValue = null;
    if (cookieStart > -1) {
      var cookieEnd = document.cookie.indexOf(";", cookieStart);
      if (cookieStart == -1) {
        alert(-2);
        cookieEnd = document.cookie.length;
      }
      cookieValue = decodeURIComponent(document.cookie.substring(cookieStart + cookieName.length, cookieEnd));
    }
    for (var i = 0; i < styleShow.length; i++) {
      if (cookieValue == styleShow[i]) {
        alert(styleShow[i]);
        return styleShow[i];
      }
    }
    return styleShow[0];
  },
  // Generate cookie
  //name cookie Name 
  //value  Value 
  //expires  Expired time 
  //path  Path 
  setCookie: function (name, value, expires, path, domain, secure) {
    var cookieText = encodeURIComponent(name) + "=" + encodeURIComponent(value) + ";";
    if (expires instanceof Date) {
      cookieText += "expires=" + expires.toGMTString();
    }
    if (path) {
      cookieText += ";path=" + path;
    }
    if (domain) {
      cookieText += ";domain=" + domain;
    }
    if (secure) {
      cookieText += ";secure";
    }
    document.cookie = cookieText;
  },
  // Reset cookie
  unsetCookie: function (name, path, domain, secure) {
    this.set(name, "", new Date(0), path, domain, screen);
  }
};

More readers interested in JavaScript can check the topics of this site: "Summary of json Operation Skills in JavaScript", "Summary of JavaScript Switching Special Effects and Skills", "Summary of JavaScript Search Algorithm Skills", "Summary of JavaScript Animation Special Effects and Skills", "Summary of JavaScript Error and Debugging Skills", "Summary of JavaScript Data Structure and Algorithm Skills", "Summary of JavaScript Traversal Algorithm and Skills" and "Summary of JavaScript Mathematical Operation Usage"

I hope this article is helpful to everyone's JavaScript programming.


Related articles: