• js获取url参数,操作url参数


    function getParam(key) {
        var tmp = location.search; 
        tmp = decodeURIComponent(tmp);
        var index = tmp.indexOf(key); 
        var length = key.length;     
        var value;
        if(index != -1){
            var start = index+length+1; 
            var end = tmp.indexOf('&',start);
            if(end == -1){
               value = tmp.slice(start);
            }
            else{
                value = tmp.slice(start,end);
            }        
        }
        else{
            value = '';
        }    
        return value;
    }
    
    console.log( getParam('userid') ); // xxx

    js 设置url参数

    setParam:function (url,key,val) {
            var link = url;
            var tmp = '';
            if(link.indexOf('?') > 0 ){
                tmp = link.substr( link.indexOf('?') );  // ?status=all
            }
            tmp = decodeURIComponent(tmp);
            link = decodeURIComponent(link);
            if(!tmp){ 
                link = link + '?' + key + '=' + val + '&'; 
            }
            else{ 
                var ind = tmp.indexOf(key);
                if(ind < 0 ){ // 不存在这个key 
                    ind = link.indexOf('?')+1;
                    var qianzhi = link.substr(0,ind);
                    var houzhi = link.substr(ind);
                    link = qianzhi + key + '=' + val + '&' + houzhi;
                }
                else{  //已有这个key,则更新这个key对应的值
                    ind = link.indexOf(key);
                    var qianzhi = link.substr(0,ind);
                    var ind2 = link.indexOf('&',ind);
                    var houzhi = link.substr(ind2);
                    link = qianzhi + key + '=' + val + houzhi;
                }
            }
            return link;
        },

    js 移除url参数

    dropParam:function (url,key) {
            var link = url;
            var tmp = '';
            if(link.indexOf('?') > 0 ){
                tmp = link.substr( link.indexOf('?') );  // ?status=all
            }
            tmp = decodeURIComponent(tmp);
            link = decodeURIComponent(link);
            if(tmp){
                if( tmp.indexOf(key) >0){
                    var ind = link.indexOf(key);
                    var qianzhi = link.substr(0,ind);
                    var ind2 = link.indexOf('&',ind);
                    var houzhi = link.substr(ind2+1);
                    link = qianzhi+houzhi;
                }
            }
            return link;
        },
  • 相关阅读:
    ie烦人的bug篇
    javascript之复习(框架里的方法们)
    javascript之判断专题
    js 值类型和引用类型
    作用域链
    javascript解析机制——预解析
    SSH做反向代理
    怎样在 CentOS 7.0 上安装和配置 VNC 服务器
    创建GitHub技术博客全攻略
    rsync 备份 CENTOS 系统!
  • 原文地址:https://www.cnblogs.com/isdom/p/webclips054.html
Copyright © 2020-2023  润新知