Location 对象是 Window 对象的一个部分,可通过 window.location 属性来访问。
举例说下 Location 对象 的属性与方法
hash:设置或返回从井号 (#) 开始的 URL(锚)
网址:http://www.youkuaiyun.com/index.html#head
返回:#head
<script type="text/javascript">
document.write(location.pathname);
</script>
host:设置或返回主机名和当前 URL 的端口号
网址:http://www.youkuaiyun.com:3389/index.html
返回:www.youkuaiyun.com:3389
hostname:设置或返回当前 URL 的主机名
网址:http://www.youkuaiyun.com:3389/index.html
返回:www.youkuaiyun.com
href:设置或返回完整的 URL
网址:http://www.youkuaiyun.com:3389/index.html
返回:http://www.youkuaiyun.com:3389/index.html
pathname:设置或返回当前 URL 的路径部分
网址:http://www.youkuaiyun.com/index.html#head
返回:/index.html
port:设置或返回当前 URL 的端口号
网址:http://www.youkuaiyun.com:3389/index.html
返回:3389
protocol:设置或返回当前 URL 的协议
网址:http://www.youkuaiyun.com:3389/index.html
返回:http:
search:设置或返回从问号 (?) 开始的 URL(查询部分)。
网址:http://www.youkuaiyun.com/index.html?id=333
返回:?id=333
assign():加载新的文档
语法:window.location.assign(URL)
reload():重新加载当前文档
语法:window.location.reload()
replace():用新的文档替换当前文档
语法:window.location.replace()

本文详细介绍了浏览器Location对象的各种属性和方法,如hash、host、hostname等,并通过实例展示了如何使用这些特性来实现页面导航、URL操作等功能。

被折叠的 条评论
为什么被折叠?



