Use js to cut URL parameters

For some development scenarios, when Jsp or freemarker and other template engines are not used, the corresponding parameters are usually obtained by cutting the url, and then the corresponding data is obtained by interacting with the background through AJAX

Here is a demo example:

test.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>切割URL</title> 

</head> 

<body> 
<a href="/LMS/test?userId=1">点击</a>
</body> 
</html> 

test2.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
<html xmlns="http://www.w3.org/1999/xhtml">  
<head>  
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
<title>三级联动效果</title>  
<style type="text/css">  
    select{ width:100px; text-align:center;}  
</style>  
<script type="text/javascript">  

    window.onload=function(){  
  
        GetRequest();
    };  
    function GetRequest() {
          var fullURL = window.location.href;
          alert(fullURL);
           var url = location.search; // Get the string after the "?" character in the url 
           var theRequest =  new Object();
           alert(url);
           if (url.indexOf("?") != -1) {
              var str = url.substr(1);
              strs = str.split("&");
              alert(strs)
              for(var i = 0; i < strs.length; i ++) {
                 theRequest[strs[i].split("=")[0]]=unescape(strs[i].split("=")[1]);
                  alert(theRequest[strs[i].split("=")[0]]);
              }
           }
           return theRequest;
        }
</script>  
</head>  
  
<body>  

<p>测试:</p>  
</body>  
</html>  

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325001849&siteId=291194637