jQuery UI - Accordion Save Index

Save the index on the accordion element of the current page into a cookie. Then when the page reloads, use that cookie.

by Doug Hill

HTML

<div id="accordion">
     <h3>DIAMOND APPLICATION</h3>

    <div>
         <h5>General Information</h5>

        <ul>
            <li>Application Record</li>
            <li>Project Tracker</li>
        </ul>
         <h5>Support</h5>

        <ul>
            <li>On-Call Schedule</li>
            <li>Datasource Contact List</li>
            <li>Documentation</li>
        </ul>
    </div>
     <h3>CE DASHBOARD APPLICATION</h3>

    <div>
         <h5>General Information</h5>

        <ul>
            <li>Application Record</li>
            <li>Project Tracker</li>
        </ul>
         <h5>Support</h5>

        <ul>
            <li>Documentation</li>
            <li>Support Contact List</li>
        </ul>
         <h5>Tools and Utilities</h5>

        <ul>
            <li><a href="#" target="_self">FAQ Administration</a>

            </li>
            <li>Change Log Administration</li>
            <li>User Access Management</li>
            <li>CEDB Developer Reports</li>
        </ul>
    </div>
     <h3>SD3 TEAM</h3>

    <div>
         <h5>General Information</h5>

        <ul>
            <li>Contact Information</li>
            <li>On-Call Schedule</li>
        </ul>
    </div>
     <h3>OTHER</h3>

    <div>
         <h5>General Information</h5>

        <ul>
            <li>List item one</li>
            <li>List item two</li>
            <li>List item three</li>
        </ul>
    </div>
</div>

JavaScript

(function (factory) {
	if (typeof define === 'function' && define.amd) {
		// AMD. Register as anonymous module.
		define(['jquery'], factory);
	} else {
		// Browser globals.
		factory(jQuery);
	}
}(function ($) {

	var pluses = /\+/g;

	function raw(s) {
		return s;
	}

	function decoded(s) {
		return decodeURIComponent(s.replace(pluses, ' '));
	}

	function converted(s) {
		if (s.indexOf('"') === 0) {
			// This is a quoted cookie as according to RFC2068, unescape
			s = s.slice(1, -1).replace(/\\"/g, '"').replace(/\\\\/g, '\\');
		}
		try {
			return config.json ? JSON.parse(s) : s;
		} catch(er) {}
	}

	var config = $.cookie = function (key, value, options) {

		// write
		if (value !== undefined) {
			options = $.extend({}, config.defaults, options);

			if (typeof options.expires === 'number') {
				var days = options.expires, t = options.expires = new Date();
				t.setDate(t.getDate() + days);
			}

			value = config.json ? JSON.stringify(value) : String(value);

			return (document.cookie = [
				config.raw ? key : encodeURIComponent(key),
				'=',
				config.raw ? value : encodeURIComponent(value),
				options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
				options.path    ? '; path=' + options.path : '',
				options.domain  ? '; domain=' + options.domain : '',
				options.secure  ? '; secure' : ''
			].join(''));
		}

		// read
		var decode = config.raw ? raw : decoded;
		var cookies = document.cookie.split('; ');
		var result = key ? undefined : {};
		for (var i = 0, l = cookies.length; i < l; i++) {
			var parts = cookies[i].split('=');
			var name = decode(parts.shift());
			var cookie = decode(parts.join('='));

			if (key && key === name) {
				result = converted(cookie);
				break;
			}

			if (!key) {
				result[name] = converted(cookie);
			}
		}

		return result;
	};

	config.defaults = {};

	$.removeCookie = function (key, options) {
		if ($.cookie(key) !== undefined)...