获取当前页面路径
var pathname = window.location.pathname;
获取当前页面完整的url
var url = window.location.href;
获取当前页面的域名(基础url)
var domain = document.domain;
获取url井号"#"后边字符串
var hash = window.location.hash;
获取当前页面url问号后边的参数(字符串)
var search = window.location.search;//例如当前页面url https://www.qianduange.cn/user/index.php?uid=1//获取到的数据是 uid=1
获取url参数的方法
let URL = "http://www.baidu.com?product='iPhone 13 Pro'&price=¥9999.00"function getUrlParams4(url){ // 引入 qs 库时会默认挂在到全局 window 的 Qs 属性上 // console.log(window) let urlStr = url.split('?')[1] let result = Qs.parse(urlStr) // 拼接额外参数 let otherParams = { num:50, size:6.1 } let str = Qs.stringify(otherParams) let newUrl = url str return {result,newUrl}}console.log(getUrlParams4(URL))
获取指定的url参数值
function getQueryString(name) { var reg = new RegExp('(^|&)' name '=([^&]*)(&|$)', 'i'); var r = window.location.search.substr(1).match(reg); if (r != null) { return unescape(r[2]); } return null;}// 这样调用:alert(GetQueryString("参数名1")); alert(GetQueryString("参数名2")); alert(GetQueryString("参数名3"));