Location 对象包含有关当前 URL 的信息。
Location 对象是 Window 对象的一个部分,可通过 window.location 属性来访问。
一、Location 对象属性
var href = window.location.href;//设置或返回完整的 URL。
var protocol = window.location.protocol;//设置或返回当前 URL 的协议。
var hostname = window.location.hostname;//设置或返回当前 URL 的主机名。
var host = window.location.host;//设置或返回主机名和当前 URL 的端口号。
var port = window.location.port;//设置或返回当前 URL 的端口号。
var pathname = window.location.pathname;//设置或返回当前 URL 的路径部分。
var search = window.location.search;//设置或返回从问号 (?) 开始的 URL(查询部分)。
var hash = window.location.hash;//设置或返回从井号 (#) 开始的 URL(锚)。
http://localhost:8082/websockettest/websocket/chat.xhtml?p1=a#test
href | http://localhost:8082/websockettest/websocket/chat.xhtml?p1=a#test |
protocol | http: |
hostname | localhost |
host | localhost:8082 |
port | 8082 |
pathname | /websockettest/websocket/chat.xhtml |
search | ?p1=a |
hash | #test |
二、Location 对象方法
1.assign()
assign() 方法可加载一个新的文档。
window.location.assign(“http://www.w3school.com.cn“)
2.location.reload(forceGet)
reload() 方法类似于你浏览器上的刷新页面按钮。
如果把该方法的参数设置为 true,那么无论文档的最后修改日期是什么,它都会绕过缓存,从服务器上重新下载该文档。
window.location.reload()
3.replace(newURL)
replace() 方法不会在 History 对象中生成一个新的记录。当使用该方法时,新的 URL 将覆盖 History 对象中的当前记录。
window.location.replace("http://www.w3school.com.cn")