zoukankan      html  css  js  c++  java
  • cookie 换肤

    jquery.Cookies.js

    /**
    * Cookie plugin
    *
    * Copyright (c) 2006 ziqiu.zhang 
    * Dual licensed under the MIT and GPL licenses:
    * http://www.opensource.org/licenses/mit-license.php
    * http://www.gnu.org/licenses/gpl.html
    * 使用举例:
        //注: 写入时,subName参数请传递空值或null
        //写入Cookies-值为字符串,即不包含子键
        $.cookie("singleKey", "", "singleKey-value", { expires: 1, path: "/", secure: false })
        //读取Cookies-根据主键
        alert("singleKey:" + $.cookie("singleKey"));
    
        //写入Cookies-值为对象,则每个属性名为子键的名称,属性值为子键值
        var subNameObj = { subName1: "aaa", subName2: "bbb", subName3: "ccc" };
        $.cookie("multiKey", "", subNameObj, { expires: 1, path: "/", secure: false });
        //读取Cookies-根据主键
        alert("multiKey:" + $.cookie("multiKey"));
        //读取Cookies-根据主键和子键
        alert("multiKey,subName1:" + $.cookie("multiKey", "subName1"));
    *
    */
    
    jQuery.cookie = function(name, subName, value, options) {
        if (typeof value != 'undefined') { // name and value given, set cookie
            options = options || {};
            if (value === null) {
                value = '';
                options.expires = -1;
            }
            var expires = '';
            if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
                var date;
                if (typeof options.expires == 'number') {
                    date = new Date();
                    date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
                } else {
                    date = options.expires;
                }
                expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
            }
            // CAUTION: Needed to parenthesize options.path and options.domain
            // in the following expressions, otherwise they evaluate to undefined
            // in the packed version for some reason...
            var path = options.path ? '; path=' + (options.path) : ';path=/';
            var domain = options.domain ? '; domain=' + (options.domain) : '';
            var secure = options.secure ? '; secure' : '';
    
            //If value is an object, each property will be a sub key;
            if (typeof value == "object") {
                var k = 0;
                var tempResult = "";
                for (var tempValue in value) {
                    if (k > 0) {
                        tempResult += "&";
                    }
                    tempResult += tempValue + "=" + encodeURIComponent(value[tempValue]);
                    k++;
                }
                value = tempResult;
            } else {
                value = encodeURIComponent(value);
            }
    
            document.cookie = [name, '=', value, expires, path, domain, secure].join('');
        } else { // only name given, get cookie
            var cookieValue = null;
            if (document.cookie && document.cookie != '') {
                var cookies = document.cookie.split(';');
                for (var i = 0; i < cookies.length; i++) {
                    var cookie = jQuery.trim(cookies[i]);
                    // Does this cookie string begin with the name we want?
                    if (cookie.substring(0, name.length + 1) == (name + '=')) {
                        cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
    
                        //Search sub key
                        if (typeof subName != 'undefined' && subName != null && subName != "") {
                            var subCookies = cookieValue.toString().split('&');
                            for (var j = 0; j < subCookies.length; j++) {
                                var subCookie = jQuery.trim(subCookies[j]);
                                if (subCookie.substring(0, subName.length + 1) == (subName + '=')) {
                                    cookieValue = decodeURIComponent(subCookie.substring(subName.length + 1));
                                    break;
                                }
                            }
                        }
    
                        break;
                    }
    
                }
            }
            return cookieValue;
        }
    };
    View Code

        <script type="text/javascript" src="/themes/admin/js/jquery.Cookies.js"></script>

    <span><strong class="fl pr" id="colorcsshf"><em class="hticons hticons-hf"></em>换肤&nbsp;<em class="colorcss defaultcss" onclick="changeTheme('');"></em><em class="colorcss bluecss" onclick="changeTheme('blue');"></em><em class="colorcss greencss" onclick="changeTheme('green');"></em><em class="colorcss yellowcss" onclick="changeTheme('yellow');"></em><em class="colorcss graycss" onclick="changeTheme('gray');"></em></strong><strong class="fl"><em class="hticons hticons-yh"></em>
    function getCookie(c_name) {
        if (document.cookie.length > 0) {  //先查询cookie是否为空,为空就return ""
            c_start = document.cookie.indexOf(c_name + "=")   
            if (c_start != -1) {
                c_start = c_start + c_name.length + 1;
                c_end = document.cookie.indexOf(";", c_start);
                if (c_end == -1) c_end = document.cookie.length;
                return unescape(document.cookie.substring(c_start, c_end));
            }
        }
        return "";
    }
    
    function setCookie(c_name, value, expiredays) {
        var exdate = new Date();
        exdate.setDate(exdate.getDate() + expiredays);
        document.cookie = c_name + "=" + escape(value) + ((expiredays == null) ? "" : ";expires=" + exdate.toGMTString());
    }
    
    function loadCss(className, isIframeMenu) {
        var container = document;
        if (isIframeMenu) {
            container = window.frames["ifrmenu"].document;
        }
        var cssTag = container.getElementById('loadCss');
        var head = container.getElementsByTagName('head').item(0);
        if (cssTag) head.removeChild(cssTag);
        if (className != '') {
            css = container.createElement('link');
            css.href = '/themes/admin/css/' + className + '.css';
            css.rel = 'stylesheet';
            css.type = 'text/css';
            css.id = 'loadCss';
            head.appendChild(css);
        }
    }
    function loadIframeCss(className) {
        var cssTag = window.frames["ifrmenu"].document.getElementById('loadCss');
        var head = window.frames["ifrmenu"].document.getElementsByTagName('head').item(0);
        if (cssTag) head.removeChild(cssTag);
        if (className != '') {
            css = window.frames["ifrmenu"].document.createElement('link');
            css.href = '/themes/admin/css/' + className + '.css';
            css.rel = 'stylesheet';
            css.type = 'text/css';
            css.id = 'loadCss';
            head.appendChild(css);
        }
    }
    
    
    function changeTheme(className) {
        $.cookie('theme', '', className, { expires: 365, path: "/login", secure: false });
        $.cookie('theme', '', className, { expires: 365, path: "/", secure: false });
        loadCss(className);
        loadCss(className,true);
    }

    blue.css/gray.css/green.css/yellow.css

  • 相关阅读:
    6次作业
    5次作业
    4次作业
    3次作业
    first 作业
    pycharm快捷键
    python基础知识记录
    Jenkins+sonar+tomcat+SVN集成搭建
    成为1个技术大牛的入门到进阶之路(学习路线图)
    记一次收到QQ邮箱钓鱼邮件经历
  • 原文地址:https://www.cnblogs.com/aimyfly/p/3548098.html
Copyright © 2011-2022 走看看