认识一下window.location.hash

  引用[china-aspx]的话说,location是javascript里面管理地址栏的内置对象.

比如loation.href是 页面的url  .但是 location.hash可以获取或设置页面的 标签值 比如http://domain/#testDemo中 咱们的location.hash 就是 #testDemo

  下面引用一个 网上的demo

一个搜索版块,功能有3个:普通搜索,高级搜索,后台管理,分别指明他们各自的hash 值:#search,#advsearch,#adminboss.  在页面初始化的时候,通过window.location.hash来判断用户需要访问的页面,也就是将要显示的版块

代码
var hash;
hash
= (!window.location.hash)?"#search":window.location.hash;
window.location.hash
= hash;
//这里我们解释一下(!window.location.hash)什么意思?首先如果当前页面的地址栏的链接地址 不包含#....的这些的话,直接取值的话,他会为空!比如这个例子,http://www.ggssl.com/直接取 alert(window.location.hash)//""空 转化为 boolean值 为 false
//
如果 http://www.ggssl.com#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页面的浏览趋于传统化了。

原文地址:https://www.cnblogs.com/NetSos/p/1870593.html