最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Back Button and Refresh with AJAX - Stack Overflow

matteradmin3PV0评论

I need a solution for the page refresh and the back button when using AJAX.

I'm using simple javascript for the AJAX implementation.

If anybody could send a code snippet I would be very grateful.

I need a solution for the page refresh and the back button when using AJAX.

I'm using simple javascript for the AJAX implementation.

If anybody could send a code snippet I would be very grateful.

Share edited Mar 1, 2009 at 8:14 Chad Birch 74.7k23 gold badges155 silver badges150 bronze badges asked Mar 1, 2009 at 8:09 jarusjarus 1,87311 gold badges44 silver badges76 bronze badges 0
Add a ment  | 

5 Answers 5

Reset to default 5

If you're using jQuery, there's the history plugin.

Here's a solution that I've used in some of my pages. Add this to pages that changes are made at.

window.onbeforeunload = function() {
     window.name = "reloader"; 
}

this triggers when you leave those pages. You can also trigger it if there were changes made. So that it won't unnecessarily reload the page that needs reloading. Then on pages that you want to get reloaded on after a the browser "back" use.

if (window.name == "reloader") {
    window.name = "no";
    location.reload();
}

this will trigger a reload on the page you need reloading to.

essentially, you need to use & monitor the hash portion of the url...

http://.../path?parms#hashpart

Whan you change the hash, iirc window.location.hash , it won't reload the page, but your ajax can monitor, and respond to it.

The onbeforeunload event can be useful to guard against refreshing but it fires if you navigate away or refresh. If you require that users login to the app you can always show a generic message advising against navigating away and refreshing. If users click your app log out button set a var to disable the warning. Could probably also make a 'Close' button that does the same thing.

Try PathJS it does not require jQuery or any other additional lib.

Post a comment

comment list (0)

  1. No comments so far