window.location.hash 使用说明

location是javascript里面管理地址栏的内置对象.javascript

好比loation.href是 页面的url .可是 location.hash能够获取或设置页面的 标签值 好比http://domain/#testDemo中 我们的location.hash 就是 #testDemo 
  下面引用一个 网上的demo 
一个搜索版块,功能有3个:普通搜索,高级搜索,后台管理,分别指明他们各自的hash 值:#search,#advsearch,#adminboss. 在页面初始化的时候,经过window.location.hash来判断用户须要访问的页面,也就是将要显示的版块 java

var hash; 
hash = (!window.location.hash)?"#search":window.location.hash; 
window.location.hash = hash; 
//这里咱们解释一下(!window.location.hash)什么意思?首先若是当前页面的地址栏的连接地址 不包含#....的这些的话,直接取值的话,他会为空!好比这个例子,http://www.jb51.net/直接取 alert(window.location.hash)//""空 转化为 boolean值 为 false 
// 若是 http://www.jb51.net#hello,world直接取 alert(window.location.hash)//#hello,world 转化为 boolean值 为 true 
//下面的就是 能够用switch判断 
//调整地址栏地址,使前进、后退按钮能使用 
switch(hash){ 
case "#search": 
show("panel1"); 
break; 
case "#advsearch": 
show("panel2"); 
break; 
case "#adminboss": 
show("panel3"); 
break; 
}

下面引用 别人的一句话: 
经过window.location.hash=hash这个语句来调整地址栏的地址,使得浏览器里边的“前进”、“后退”按钮能正常使用(实质上欺骗了浏览器)。而后再根据hash值的不一样来显示不一样的面板(用户能够收藏对应的面板了),这就使得Ajax页面的浏览趋于传统化了。浏览器