获得http://localhost:8081/goods_detail.htm?type=0&goodsId=2的 参数
function getParam(){ //通过substring方法进行截取?后的字符串 var paramStr = window.location.search.substring(1); //分割&符号 var arr = paramStr .split('&') var params = {} //进行循环处理 for (var i = 0; i < arr.length; i++) { var pair = arr[i].split('=') params[pair[0]] = pair[1] } console.log(params) return params }
- 使用:
var param = getParam(); var type = param.type; var goodsId = param.goodsId;
- 通过model对象传值获取方法(thymeleaf)
<script th:inline="javascript"> var param= [[${model中定义的值}]]; </script>
- 其他参考
//协议名称 var scheme = [[${#request.getScheme()}]]; //服务器名称 var serverName = [[${#request.getServerName()}]]; //服务器端口号 var serverPort = [[${#request.getServerPort()}]]; //上下文根 var contextPath = [[${#request.getContextPath()}]]; //获取请求参数 var queryString = [[${#request.queryString}]]; //获取请求路径(不带参数) var requestURL = [[${#request.requestURL}]];