关于Location对象的总结

关于Location对象的总结

在W3C中,Locationwindow下的一个对象,所以在使用Location时,可以省略写window,直接写成location.herf酱紫就可以。既然是对象,那么就有它对应的属性和方法,下面我们来一一介绍。

Location 对象属性
属性描述
hash设置返回从井号 (#) 开始的 URL(锚)
host设置返回主机名和当前 URL 的端口号
hostname设置返回当前 URL 的主机名
href设置返回完整的 URL。
pathname设置返回当前 URL 的路径部分
port设置返回当前 URL 的端口号
protocol设置返回当前 URL 的协议
search设置返回从问号 (?) 开始的 URL(查询部分)



Location对象方法
方法描述
assign()加载新的文档。
reload()重新加载当前文档。
replace()用新的文档替换当前文档。

tips:请注意,很多人都知道这些属性的返回值,但是往往会忽略掉,其实这些属性都是可以设置的


-------------------------------------------------------------------------------------Example-------------------------------------------------------------------------------------------

属性:

hash

<script>
	/*************************hash**********************/
     // 假如现在的URL='http://supergiker.com/index.html#123456'
	alert(location.hash);// 返回的是 #123456
</script>


host

<script>
	/*************************host**********************/
	// 假如现在的URL='http://supergiker.com:8090/index.html'
	var Host=location.host;
	var arr=Host.split(':');
	var hostname=arr[0];
	var port=arr[1]
	alert(Host);//弹出 supergiker.com:8090
	alert(hostname);//弹出 supergiker.com
	alert(port);//弹出 8090
</script>


hostname

<script>
        /*************************hostname**********************/
        // 假如现在的URL='http://supergiker.com'
	var domain=location.hostname;
	alert(domain)//弹出 supergiker.com
</script>


href

<script>
	/*************************href**********************/
	// 假如现在的URL='http://supergiker.com'
	var URL=location.href;
	alert(URL)//弹出 http://supergiker.com
</script>


pathname

<script>
	/*************************pathname**********************/
	// 假如现在的URL='http://supergiker.com/images/db.jpg'
	var pathname=location.pathname;
	alert(pathname)//弹出 /images/db.jpg
</script>

port

<script>
	/*************************port**********************/
	// 假如现在的URL='http://supergiker.com:8090'
	var port=location.port;
	alert(port)//弹出 8090
</script>


protocol

<script>
	/*************************protocol**********************/
	// 假如现在的URL='http://supergiker.com'
	var protocol=location.protocol;
	alert(protocol)//弹出 http:
</script>


search

<script>
	/*************************search**********************/
	// 假如现在的URL='http://supergiker.com/index.html?url=bigImg'
	var search=location.search;
	alert(search)//弹出 ?url=bigImg
</script>

方法:
reload()
<script>
	/*************************reload()**********************/
	var newBtn=document.createElement('button');
	var bodyChild=document.body.children;
	if(bodyChild.length!=0){
		document.body.insertBefore(newBtn,bodyChild[0]);
	}else{
		document.body.appendChild(newBtn);
	}
	newBtn.οnclick=function () {
		location.reload();//重新加载页面
	}
</script>

assign()
<script>
	/*************************assign()**********************/
	var newBtn=document.createElement('button');
	var bodyChild=document.body.children;
	if(bodyChild.length!=0){
		document.body.insertBefore(newBtn,bodyChild[0]);
	}else{
		document.body.appendChild(newBtn);
	}
	newBtn.οnclick=function () {
		location.assign('http://www.supergiker.com');
		//加载新文档,当用history.go(-1)跳转时,会跳转到你点击按钮的这个页面
	}
</script>

replace()
<script>
	/*************************replace()**********************/
	var newBtn=document.createElement('button');
	var bodyChild=document.body.children;
	if(bodyChild.length!=0){
		document.body.insertBefore(newBtn,bodyChild[0]);
	}else{
		document.body.appendChild(newBtn);
	}
	newBtn.οnclick=function () {
		location.replace('http://www.supergiker.com');
		/*加载新文档,当用history.go(-1)跳转时,会跳转到你点击按钮这个页面的之前的一个页面,点击按钮的这个
		页面应该是history.go(0)*/
	}
</script>






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值