User:Mike Dillon/Scripts/cookies.js

From Wiktionary, the free dictionary
Jump to navigation Jump to search

Note: You may have to bypass your browser’s cache to see the changes. In addition, after saving a sitewide CSS file such as MediaWiki:Common.css, it will take 5-10 minutes before the changes take effect, even if you clear your cache.

  • Mozilla / Firefox / Safari: hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (Command-R on a Macintosh);
  • Konqueror and Chrome: click Reload or press F5;
  • Opera: clear the cache in Tools → Preferences;
  • Internet Explorer: hold Ctrl while clicking Refresh, or press Ctrl-F5.

function readCookie(name) {
    if (!name) return;
    var nameMatch = name.toLowerCase() + "=";
    var cookies = document.cookie.split(/\s*;\s*/);
    for (var i in cookies) {
        if (cookies[i].toLowerCase().indexOf(nameMatch) == 0) {
            return cookies[i].substr(nameMatch.length);
        }
    }
}

function writeCookie(name, value, options) {
    if (value.indexOf(";") != -1) throw "Cookie value cannot contain semi-colons";

    if (!options) options = {};

    var cookie = name + "=" + value;
    if (options.domain) cookie += ";domain=" + options.domain;
    if (options.path) cookie += ";path=" + options.path;
    if (options.expires || options.expiresInDays > 0) {
        var expires = options.expires;
        if (!expires) {
            expires = new Date(new Date().getTime() + options.expiresInDays * 86400 * 1000);
        }
        if (expires.toGMTString) expires = expires.toGMTString();
        cookie += ";expires=" + expires;
    }

    document.cookie = cookie;

    return cookie;
}

function deleteCookie(name, options) {
    if (!options) options = {};

    var epoch = new Date();
    epoch.setTime(0);
    options.expires = epoch;

    return writeCookie(name, "", options);
}