For example, loation. href is the url of the page. However, location. hash can be used to obtain or set the tag value of the page. For example, in http: // domain/# testDemo, our location. hash is # testDemo.
The following is an online demo.
A search Section has three functions: normal search, advanced search, and background management. They respectively specify their respective hash values: # search, # advsearch, # adminboss. during page initialization. location. hash to determine the page to be accessed by the user, that is, the section to be displayed
Copy codeThe Code is as follows:
Var hash;
Hash = (! Window. location. hash )? "# Search": window. location. hash;
Window. location. hash = hash;
// Here we will explain (! Window. location. hash) What does it mean? First, if the link address in the address bar of the current page does not include those of #..., it will be blank if the value is taken directly! For example, http://www.jb51.net/directly retrieves alert (window. location. hash) // "" empty to false
// If the http://www.jb51.net # hello, world directly retrieves alert (window. location. hash) // # hello, world is converted to true
// You can use the switch to determine the following:
// Adjust the address bar to enable the forward and backward buttons.
Switch (hash ){
Case "# search ":
Show ("panel1 ");
Break;
Case "# advsearch ":
Show ("panel2 ");
Break;
Case "# adminboss ":
Show ("panel3 ");
Break;
}
Here is a sentence from someone else:
You can use the window. location. hash = hash statement to adjust the address in the address bar, so that the "Forward" and "back" buttons in the browser can be used properly (actually deceiving the browser ). Then, different panels are displayed based on the hash value (you can add the corresponding panel to favorites), which makes the browsing of Ajax pages more traditional.