zoukankan      html  css  js  c++  java
  • 获取url参数,删除url参数,添加url参数

    const url = `http://localhost:7127/dashboard/workplace?a=123&b=456&c=789`
    // 获取URL参数
    function getParams(url) {
        var strs;
    	var url = url || window.location.href; //获取url中"?"符后的字串
    	var theRequest = new Object();
    	if (url.indexOf("?") != -1) {
    		url = url.substr(url.indexOf("?"));
    		var str = url.substr(1);
    		strs = str.split("&");
    		for (var i = 0; i < strs.length; i++) {
    			var index = strs[i].indexOf("=");
    			theRequest[strs[i].slice(0, index)] = unescape(strs[i].slice(index + 1, strs[i].length));
    		}
    	}
    	return theRequest;
    }
    console.log('获取url参数',JSON.stringify(getParams(url)))
    
    // 删除URL参数
    function delParam(paramKey,myurl) {
    	var url = myurl || window.location.href; //页面url
        var urlParam = window.location.search.substr(1); //页面参数
        if(myurl && url.indexOf("?")>-1) {
            urlParam = url.substr(url.indexOf("?")+1,url.length); 
        } else {
            return url
        }
    
        // 主页面地址(参数之前地址)
    	var beforeUrl = url.substr(0, url.indexOf("?")); 
    
        // 参数(主地址后面)
    	var nextUrl = "";
    
        // 
    	var arr = new Array();
    	if (urlParam != "") {
    		var urlParamArr = urlParam.split("&"); //将参数按照&符分成数组
    		for (var i = 0; i < urlParamArr.length; i++) {
                //将参数键,值拆开
    			var paramArr = urlParamArr[i].split("="); 
    			//如果键雨要删除的不一致,则加入到参数中
    			if (paramArr[0] != paramKey) {
    				arr.push(urlParamArr[i]);
    			}
    		}
    	}
    
        // 构造参数字符串
    	if (arr.length > 0) {
    		nextUrl = "?" + arr.join("&");
    	}
    
        // 拼接
    	url = beforeUrl + nextUrl;
    	return url;
    }
    console.log('删除url参数',delParam('b',url))
    
    // 添加URL参数
    function addParams(params,myurl) {
        var url = myurl || window.location.href; //页面url
        var arr = new Array()
        var nextUrl = ''
        Object.keys(params).map(item => {
            arr.push(`${item}=${params[item]}`)
        })
        if(url.indexOf('?')>-1) {
            nextUrl = "&" + arr.join("&");
        } else {
            nextUrl = "?" + arr.join("&");
        }
        return url + nextUrl
    }
    
    console.log('添加url参数',addParams({d:135,e:246},url))
    
  • 相关阅读:
    Shell编程-05-Shell中条件测试与比较
    Shell编程-04-Shell中变量数值计算
    Shell编程-03-Shell中的特殊变量和扩展变量
    Shell编程-02-Shell变量
    Shell编程-01-Shell脚本初步入门
    Windows与Linux相互远程桌面连接
    awk基础05-自定义函数和脚本
    使用Kafka Connect创建测试数据生成器
    设置KAFKA
    Apache Kafka使用默认配置执行一些负载测试来完成性能测试和基准测试
  • 原文地址:https://www.cnblogs.com/chifung/p/13563255.html
Copyright © 2011-2022 走看看