示例URL
http://127.0.0.1:8080/testdemo/test.html?id=1&name=test
// 返回 web 主机的域名,如:http://127.0.0.1:8080/testdemo/test.html?id=1&name=test var url = window.location.href; // 返回所使用的 web 协议,如:http: var protocol = window.location.protocol; // 返回当前页面的路径和文件名,如:/testdemo/test.html var pathname = window.location.pathname; // 获取主机地址和端口号, 如:http://127.0.0.1:8080 var host = window.location.host; // 获取主机地址, 如:http://127.0.0.1/ var hostname = window.location.hostname; // 返回 web 主机的端口,如:8080 var port = window.location.port; // 获取参数,如:?id=1&name=test var search = window.location.search; // 获取锚点(“#”后面的分段) var hash = window.location.hash; // 通过参数名获取url中的参数值* var id = urlSearch('id'); // 1 var name = urlSearch('name'); // test function urlSearch(parmas) { let href = window.location.href let query = href.substring(href.indexOf('?')+1); let vars = query.split("&"); for (var i = 0; i < vars.length; i++) { let pair = vars[i].split("="); if(pair[0] == parmas){ return pair[1]; } } }