window.location对象

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方法打开新页面后,无法用浏览器的后退按钮返回到原来的页面。

 

 

posted on 2013-04-22 14:08  花森  阅读(301)  评论(0编辑  收藏  举报