Home  >  Article  >  Web Front-end  >  window.location.hash usage instructions_javascript skills

window.location.hash usage instructions_javascript skills

WBOY
WBOYOriginal
2016-05-16 18:16:521387browse

For example, location.href is the URL of the page. But location.hash can get or set the tag value of the page. For example, our location.hash in http://domain/#testDemo is #testDemo
The following is a reference to an online demo
A search section with three functions: normal search, advanced search, and background management. Specify their respective hash values: #search, #advsearch, #adminboss. When the page is initialized, it is judged by window.location.hash The page that the user needs to visit is the section that will be displayed

Copy the code The code is as follows:

var hash;
hash = (!window.location.hash)?"#search":window.location.hash;
window.location.hash = hash;
//Here we explain (! What does window.location.hash) mean? First of all, if the link address in the address bar of the current page does not contain #...., if the value is taken directly, it will be empty! For example, in this example, http://www. jb51.net/Directly take alert(window.location.hash)//""empty is converted into boolean value is false
// If http://www.jb51.net#hello,world directly take alert(window. location.hash)//#hello,world is converted into a boolean value of true
//The following can be judged by switch
//Adjust the address bar address so that the forward and back buttons can use
switch( hash){
case "#search":
show("panel1");
break;
case "#advsearch":
show("panel2");
break ;
case "#adminboss":
show("panel3");
break;
}

The following quote from someone else:
Through window The statement .location.hash=hash is used to adjust the address in the address bar so that the "forward" and "back" buttons in the browser can be used normally (essentially deceiving the browser). Then different panels are displayed based on different hash values ​​(the user can collect the corresponding panels), which makes the browsing of Ajax pages more traditional.
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn