Mather

We create our own demons.

项目小记

默认分类 0 评

解析 URL

移动端兼容性较好(不适用于 IE 浏览器!)

    var _url            = new URL(window.location.href);
    var _version        = _url.searchParams.get("version");
    var _refer          = _url.searchParams.get("refer");

另一种是使用 js-url 库来解析 URL

    url('#poo');      // undefined 尝试获取不存在的参数
    if (url('?') && url('?').refer){}

比较版本号

/**
 * Simply compares two string version values.
 *
 * Example:
 * versionCompare('1.1', '1.2') => -1
 * versionCompare('1.1', '1.1') =>  0
 * versionCompare('1.2', '1.1') =>  1
 * versionCompare('2.23.3', '2.22.3') => 1
 *
 * Returns:
 * -1 = left is LOWER than right
 *  0 = they are equal
 *  1 = left is GREATER = right is LOWER
 *  And FALSE if one of input versions are not valid
 *
 * @function
 * @param {String} left  Version #1
 * @param {String} right Version #2
 * @return {Integer|Boolean}
 * @author Alexey Bass (albass)
 * @since 2011-07-14
 */
versionCompare = function (left, right) {
    if (typeof left + typeof right != 'stringstring')
        return false;

    var a   = left.split('.')
        , b = right.split('.')
        , i = 0, len = Math.max(a.length, b.length);

    for (; i < len; i++) {
        if (( a[i] && !b[i] && parseInt(a[i]) > 0 ) || ( parseInt(a[i]) > parseInt(b[i]) )) {
            return 1;
        } else if (( b[i] && !a[i] && parseInt(b[i]) > 0 ) || ( parseInt(a[i]) < parseInt(b[i]) )) {
            return -1;
        }
    }

    return 0;
}

实例

    if (typeof _version != 'null' && versionCompare(_version, '0.9.9')) {}

Laravel X-CSRF-TOKEN 请求头

   <meta name="csrf-token" content="{{ csrf_token() }}">
    $.ajaxSetup({
        headers:
            {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')}
    });

HTTP 协议小结

发表评论
撰写评论