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" buttons 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 ":...}

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.

Related Article

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: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.