Location is the built-in object for managing the address bar in Javascript. For example, location. href manages the URL of the page and uses location. href = URL to directly redirect the page url. Location. Hash can be used to obtain or set the tag value of the page. For example, http: // domain/# admin's location. Hash = "# admin ". This attribute value can be used to do a very meaningful thing. Many people like to add webpages to favorites for future browsing. However, for an Ajax page, a page is generally used to process all transactions. That is to say, if you browse interesting content on an Ajax page, you want to add it to favorites, however, there is only one address. Next time you open this address, you still need to constantly click the webpage as before to find the page you love. In addition, the "Forward" and "backward" button on the browser will also become invalid, which is a huge obstacle for many users who are used to traditional pages. So, how to use location. Hash to solve these two problems? It is actually not mysterious at all. For example, my author management system has three main functions: normal search, advanced search, and background management. I assign them a hash value: # search, # advsearch, and # Admin. During page initialization. location. hash to determine the page to be accessed by the user, and then adjust the display page through JavaScript. For example: var hash; hash = (! Window. Location. Hash )? "# Search": window. location. hash; window. location. hash = hash; // adjust the address bar address so that the forward and backward buttons can use switch (hash) {Case "# search": selectpanel ("pnlsearch "); // display the normal search panel break; Case "# advsearch ":... case "# admin ":...}
Use window. location. hash = hash to adjust the address in the address bar, so that the "Forward" and "back" buttons in the browser can be used normally (in essence, the browser is cheated ). 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.