window.location对象

来源:互联网 发布:淘宝模特韩国短发 编辑:程序博客网 时间:2024/06/17 14:36

location是window下的一个全局变量

location = {  hash: "",  host: "",  hostname: "",  href: "",  pathname: "",  port: "",  protocol: "",  search: "",  reload: funciton(){},  replace: function(){},  assign: function(){}};

例如:当前的url为:“http://127.0.0.1:8080/PMS/index.html?testsearch#testhash",

则location的内容是:

location = {  hash: "#testhash",  host: "127.0.0.1:8080",  hostname: "127.0.0.1",  href: "http://127.0.0.1:8080/PMS/index.html?testsearch#testhash",  pathname: "/PMS/index.html",  port: "8080",  protocol: "http:",  search: "?testsearch",  reload: function(){},  replace: function(){},  assign: function(){}};

这里需要需要注意两点:
1. location.search 和 location.hash
如果一个url的后面同时存在'#'和'?',若‘#’在'?'前面,则location.search 返回 ""。也就是说location.hash会返回'#'后面的所有内容,包括‘?’,而location.search返回‘?’后面的内容,但遇到‘#’就截止。
2. location.replace(url)和location.assign(url)
location.assign实际上和location.href是一样的,而location.replace不会在浏览器的url历史记录里留下痕迹,也就是说,用location.replace方法打开新页面后,无法用浏览器的后退按钮返回到原来的页面。