用法:addUrlParam('id','10')

效果:https://www.baidu.com/?id=10

function addUrlParam(name, value) { //地址中加参数
            	var currentUrl = window.location.href.split('#')[0];
            	if (/\?/g.test(currentUrl)) {
            		if (/name=[-\w]{4,25}/g.test(currentUrl)) {
            			currentUrl = currentUrl.replace(/name=[-\w]{4,25}/g, name + "=" + value);
            		} else {
            			currentUrl += "&" + name + "=" + value;
            		}
            	} else {
            		currentUrl += "?" + name + "=" + value;
            	}
            	if (window.location.href.split('#')[1]) {
            		window.location.href = currentUrl + '#' + window.location.href.split('#')[1];
            	} else {
            		window.location.href = currentUrl;
            	}
} 

原文地址:https://course.51qux.com/1828.html