JS向URL中添加数据

<!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>

猜你喜欢

转载自blog.csdn.net/qq_37164847/article/details/80282861