Use the hash value of location to solve Ajax Problems

Source: Internet
Author: User

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.

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.