location.host及获取url参数

1:location.host
host 属性是一个可读可写的字符串,可设置或返回当前 URL 的主机名称和端口号。
返回当前URL的主机名和端口
地址:http://www.runoob.com/try/try.php?filename=tryjsref_loc_host

结果:
www.runoob.com

2、 获取url参数
const getQueryString = function (name) {
var reg = new RegExp(’(^|&)’ + name + ‘=([^&]*)(&|$)’, ‘i’)
var r = location.search.substr(1).match(reg)
if (r != null) return decodeURI(r[2])
return null
}
http://www.baidu.com/try/try.php?report=abcd
console.log(getQueryString(‘report’))
结果:
abcd

猜你喜欢

转载自blog.csdn.net/xuqingbaobao/article/details/88825895