window.location对象详解方法使用

创建:2019年12月19日 修改:2019年12月21日  作者:清风明月

window.location.href(当前URL)
结果:http://www.myurl.com:8081/test/?id=123&username=sun#part

window.location.protocol(协议)
结果:http:

window.location.host(域名 + 端口)
结果:www.myurl.com:8081

window.location.hostname(域名)
结果:www.myurl.com

window.location.port(端口)
结果:8081

window.location.pathname(路径部分)
结果:/test/

window.location.search(请求的参数)
结果:?id=123&username=sun

window.location.hash(锚部分)
结果:#part

window.location.origin(“?”前边的URL)
结果:http://www.myurl.com:8081


//获取URL参数,strGetUrlArg(参数名,默认值)
function strGetUrlArg(sv,sd){
	var s=window.location.search
	var a=s.match(new RegExp("(^|&|\\?)"+sv+"=[^&]*"))
	return decodeURIComponent(a==null||sv==""?(sd||""):a[0].slice(sv.length+2))
}
//使用
strGetUrlArg("name","程序家")