location 属性:
实例url: http://localhost:8080/org/group/index?fromString=bctop#part2
location.href: http://localhost:8080/org/group/index?fromString=bctop#part2
location.protocol: http:
location.hostname: localhost
location.port: 8080
location.host: localhost:8080
location.pathname: /org/ group/index
location.search: ?fromString=bctop
location.hash: #part2
说明:
- 如果 port 不为空,则 host = hostname + ":" + port;如果 port 为空,则 host = hostname。一般来说我们都使用默认端口 80,所以 port 一般为空。
- hash 是包含 # 的。
- pathname 是包含 / 的。
- search 是包含 ? 的。
- protocol 是包含 : 的。
- 以上属性均是可读可写的。
- 如果直接对 location 取值赋值,就相当于对 location.href 取值赋值。
location 方法:
location.assign(newURL) : 加载新的文档。 等同于 location.href = newURL;
location.reload(true/false) : 重新加载当前文档,参数默认为 false,表示从缓存中重新读取;如果为 true,表示从服务端重新读取。
location.replace(newURL) : 用新的文档替换当前文档。
assign() 和 replace() 的区别:
- 假设有 assign.htm 用 assign() 跳转到 history.htm,那么在 history.htm 的 JavaScript 对象 history 中就会记录两条历史记录;
- 假设有 replace.htm 用 replace() 跳转到 history.htm,那么在 history.htm 的 JavaScript 对象 history 中就只有一条历史记录(该记录为 history.htm,而不是 replace.htm)
详细说明参照网站:http://www.w3school.com.cn/htmldom/dom_obj_location.asphttp://red-apple.blog.163.com/blog/static/201165061201262314453766/