<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>测试</title>
</head>
<body>
<button id="aa">点击</button>
<script type="text/javascript">
var dianji=document.getElementById("aa");
dianji.onclick=function(){
alert("点击");
addUrlPara("大海","Page01");
}
function addUrlPara(name, value) {
var currentUrl = window.location.href.split('#')[0];
console.log(currentUrl);
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;
}
}
</script>
</body>
</html>
<html>
<head>
<meta charset="UTF-8">
<title>测试</title>
</head>
<body>
<button id="aa">点击</button>
<script type="text/javascript">
var dianji=document.getElementById("aa");
dianji.onclick=function(){
alert("点击");
addUrlPara("大海","Page01");
}
function addUrlPara(name, value) {
var currentUrl = window.location.href.split('#')[0];
console.log(currentUrl);
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;
}
}
</script>
</body>
</html>