Some pages that are frequently used by AJAX are sometimes not dare to be refreshed, because after refreshing, you may see a very different page from the original one. I will not discuss whether to use AJAX in the case of a large number of page content updates. This article briefly describes the functions such as browser forward, backward, and refresh.
Assume that a page with two Tabs entries contains a large amount of text and images. If you think the content of Tab2 is good, add it to favorites or send it to friends. The next time you open the link via favorites or click a friend to open the link, you will probably see the content of Tab1. Then you need to click Tab2 again to see the content you want to see. If the page logic is more complex, you may need to perform multiple steps to return to the desired content. This is a poor experience.
To make functions such as refreshing and adding to favorites work properly, the current operation must be reflected on the URI. However, changing the URI does not cause page refresh. Therefore, you can change the fragment in the URI. For example, after you click Tab1, change the URI to signature.
Copy codeThe Code is as follows: function ShowTab1 (){
$ ("# Tab2"). hide ();
$ ("# Tab1"). show ();
Window. location. hash = "# tab1 ";
};
Function ShowTab2 (){
$ ("# Tab1"). hide ();
$ ("# Tab2"). show ();
Window. location. hash = "# tab2 ";
};
This has changed the URI,.Copy codeThe Code is as follows: $ (document). ready (ShowTab ());
Function ShowTab (){
If (window. location. hash = "# tab2 ")
ShowTab2 ();
Else
ShowTab1 ();
}
In this way, functions such as refreshing and adding to favorites can be used, but it is still troublesome to move forward and backward. Although these two buttons have become available, the content of the webpage does not change when you click them. We need to use the onhashchange event of the body. The onhashchange event is not supported by all browsers. If the browser that does not support this event also detects the changes in the content after #, you may need to write a function to regularly detect the window. location. hash changes or onhashchange events.
Sample Code package and download (Visual Studio 2010)