简体   繁体   中英

Force refresh/reload when user navigates Back and forward again

I want to know how i can ensure page reload/refresh when user presses the back button and then presses forward again.? Please help!

You can use cookie to set "back button pressed" flag.

eg. jQuery + jquery cookie plugin

    $(document).ready(function($) {
      if ($.cookie("reload") === *YOUR-PAGE-ID*) {

        $.removeCookie("reload");
        location.reload();

      } else if (window.history && window.history.pushState) {

        window.history.pushState("forward", null, "./#forward");

        $(window).on("popstate", function() {
          $.cookie("reload", *YOUR-PAGE-ID*); // set cookie with some page id
        });

      }
    });

Since popstate is not supported by older browsers you should use history.js

<input type='button' onclick="window.location=window.location.href;">

演示在我的网站http://vitideas.in/stackoverflow/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM