Window.history saves the user's website access record during a session. A new history record is created every time the user accesses a new URL.
history.go(), history.back(), history.forward()history.back() and history.forward() represent one page backward and one page forward respectively. history.go(num) indicates how many pages to turn forward or backward. If num is a positive number, it means turning forward, and if it is a negative number, it means turning backward.
Windows window object (history) history.go(), history.back(), history.forward().
Because windows object reference is not necessary. So windows.history.go() == history.go().
The go() method has only one parameter, which can be an integer or a negative number. If it's positive, move forward. Negative numbers mean going backwards. (Equivalent to the difference between Forward and Back)
Therefore, to go back one page, you can use the following code: (To go back multiple pages, just change the parameters of go)
window.history.go(-1);
To go forward one page, You only need to use positive numbers;
history.go(1);
In addition, you can use the back() and forward() methods to achieve the same operation:
history.back(); 后退 history.forward(); 前进
You can also use the length attribute to view the number of pages in the history:
history.length;
2. The difference between history.go(-1) and history.back()
history.go(-1)表示后退与刷新。如数据有改变也随之改变 history.back()只是单纯的返回到上一页。
Example:
Html code
Js code
$("#btn_exit").bind("click",function(){ window.location.reload(); //刷新 window.history.go(1); //前进 window.history.go(-1); //返回+刷新 window.history.forward(); //前进 window.history.back(); //返回 });
【Related Recommendations】
1.Special Recommendation:"php Programmer Toolbox" V0.1 version Download
2.Usage of window.history in js (1)
3.Detailed introduction to history.pushState() in h5 Usage examples
4.In-depth understanding of the history features in h5-pushState, replaceState
5.The impact of the History API in h5 on web applications
The above is the detailed content of Usage of window.history in js (2). For more information, please follow other related articles on the PHP Chinese website!