Authored by 姜枫

add common.js

  1 +/**
  2 + * 页面公共逻辑和接口
  3 + * @author: xuqi<qi.xu@yoho.cn>
  4 + * @date: 2015/11/23
  5 + */
  6 +
  7 +function cookie(name) {
  8 + var re = new RegExp(name + '=([^;$]*)', 'i'),
  9 + matchPattern = '$1';
  10 +
  11 + return re.test(decodeURIComponent(document.cookie)) ? RegExp[matchPattern] : '';
  12 +}
  13 +
  14 +function setCookie(name, value, options) {
  15 + var expires = '',
  16 + path,
  17 + domain,
  18 + secure,
  19 + date;
  20 +
  21 + if (typeof value !== 'undefined') {
  22 + options = options || {};
  23 + if (value === null) {
  24 + value = '';
  25 + options.expires = -1;
  26 + }
  27 +
  28 + if (options.expires &&
  29 + (typeof options.expires === 'number' || options.expires.toUTCString)) {
  30 + if (typeof options.expires === 'number') {
  31 + date = new Date();
  32 + date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
  33 + } else {
  34 + date = options.expires;
  35 + }
  36 + expires = '; expires=' + date.toUTCString();
  37 + }
  38 + path = options.path ? '; path=' + options.path : '';
  39 + domain = options.domain ? '; domain=' + options.domain : '';
  40 + secure = options.secure ? '; secure' : '';
  41 + document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
  42 + }
  43 +}
  44 +
  45 +function queryString() {
  46 + var vars = {},
  47 + hash,
  48 + i,
  49 + search = window.location.search,
  50 + hashes = search ? decodeURIComponent(search).slice(1).split('&') : [];
  51 +
  52 + for (i = 0; i < hashes.length; i++) {
  53 + hash = hashes[i].split('=');
  54 + vars[hash[0]] = hash[1];
  55 + }
  56 + return vars;
  57 +}
  58 +
  59 +window.cookie = cookie;
  60 +
  61 +window.setCookie = setCookie;
  62 +
  63 +window.queryString = queryString;