美文网首页
获取参数等等

获取参数等等

作者: web佳 | 来源:发表于2018-07-17 16:26 被阅读0次
      var util = {
                /**
                 * 获取url参数.
                 * @param {String} [name] 参数名称,无此参数时返回所有参数
                 * @return {String|Object} name存在时返回相应的值,否则返回所有参数
                 */
                getUrlParam: function (name) {
                    var url = window.location.search.substr(1)||window.location.hash.substr(1);
                    if (!url) {
                        return '';
                    }
                    if (name) {
                        var value = new RegExp('(?:^|&)' + name + '=([^&]*)(&|$)', 'g').exec(url);
                        return util.htmlEncodeAll(value && window.decodeURIComponent(value[1]) || '');
                    }
                    var result = {};
                    var reg = /(?:^|&)([^&=]+)=([^&]*)(?=(&|$))/g;
                    var item;
                    /* jshint boss: true */
                    while (item = reg.exec(url)) {
                        result[item[1]] = util.htmlEncodeAll(window.decodeURIComponent(item[2]));
                    }
                    return result;
                },
                /**
                 * 过滤html中的特殊符号
                 * @param  {String} [e] 待过滤的html
                 * @return {String}  返回过滤后的html
                 */
                htmlEncodeAll: function (e) {
                    return null == e ? "" : e.replace(/\</g, "&lt;").replace(/\>/g, "&gt;").replace(/\&/g, "&amp;").replace(/"/g, "&quot;").replace(/'/g, "'");
                },
                /**
                 * 添加script.
                 * @param {String} url js url
                 * @param {Function} [onload] 加载成功回调
                 * @param {Function} [onerror] 加载失败回调
                 * @return {HTMLElement} script引用
                 */
                addScript: function (url, onload, onerror) {
                    var script = document.createElement('script');
                    if (onload) {
                        script.onload = function () {
                            onload(script);
                        };
                    }
                    script.onerror = function () {
                        if (onerror) {
                            onerror(script);
                        } else if (onload) {
                            onload(script);
                        }
                    };
                    script.src = url;
                    document.head.appendChild(script);
                    return script;
                },
                /**
                 * 复制对象属性.
                 * @param {Object} toObj 复制到此对象
                 * @param {Object} fromObj 要复制的对象
                 */
                extend: function (toObj, fromObj) {
                    for (var key in fromObj) {
                        if (fromObj[key] !== 'undefined') {
                            toObj[key] = fromObj[key];
                        }
                    }
                },
                // Check if documentElement already has a given class.
                hasClass: function (node, className) {
                    var regex;
                    regex = new RegExp(className, 'i');
                    return node.className.match(regex);
                },
    
                // Add one or more CSS classes to the <html> element.
                addClass: function (node, className) {
                    var currentClassNames = null;
                    if (!this.hasClass(node, className)) {
                        currentClassNames = node.className.replace(/^\s+|\s+$/g, '');
                        node.className = currentClassNames + " " + className;
                    }
                },
    
                // Remove single CSS class from the <html> element.
                removeClass: function (node, className) {
                    if (this.hasClass(node, className)) {
                        node.className = node.className.replace(" " + className, "");
                    }
                },
                /**
                 * 为url添加变量.
                 * @param {String} url
                 * @param {String|Object} name
                 *    为字符串类型时参数作为新增参数的名称,第三个参数不能缺省
                 *    为对象类型时参数为要增加的参数集合,属性为参数名称,值为参数值
                 * @param {String} value 变量值
                 * @return {String} 新的url
                 */
                urlAddParam: function (url, name, value) {
                    // 分割url,arr[1] 为头部,arr[2]为参数,arr[3]为hash
                    var arr = url.match(/([^\?#]*\??)([^#]*)?(#.*)?/);
                    var prefix = arr[1];
                    var param = arr[2];
    
                    if (param) {
                        prefix += param + '&';
                    } else if (arr[1].indexOf('?') === -1) {
                        prefix += '?';
                    }
                    var newParam = '';
                    if (typeof name === 'object') {
                        for (var key in name) {
                            newParam += '&' + key + '=' + encodeURIComponent(name[key]);
                        }
                        newParam = newParam.substr(1);
                    } else {
                        newParam = name + '=' + encodeURIComponent(value);
                    }
                    return prefix + newParam + (arr[3] || '');
                },
                /**
                 *模拟a标签跳转
                 *@param {string} url 要跳转的url
                 *@param {Boolean} isReplace 是否替换当前页
                 */
                locationRedirect: function (url, isReplace) {
                    if (isReplace) {
                        this.locationReplace(url);
                    }
                    if (os.ios) {
                        var $lr = $("#locationRedirect"),
                            ev = document.createEvent('HTMLEvents');
                        ev.initEvent('click', false, true);
                        if ($lr.length) {
                            $lr.attr("href", url);
                        } else {
                            $lr = $('<a id="locationRedirect" href="' + url + '" style="display:none;"></a>');
                            $(document.body).append($lr);
                        }
                        $lr[0].dispatchEvent(ev);
                    } else {
                        window.location.href = url;
                    }
                },
                /**
                 * 修复iOS 9.0和9.1 下 replace后 再次跳转页面后调用back会回到前2个页面.
                 * @param {String} url replace 的url
                 */
                locationReplace: function (url) {
                    if (os.ios && /^9\.[01]/.test(os.version)) {
                        history.replaceState({}, null, url);
                        location.reload();
                    } else {
                        location.replace(url);
                    }
                },
            };
    
    

    相关文章

      网友评论

          本文标题:获取参数等等

          本文链接:https://www.haomeiwen.com/subject/rzkgpftx.html