Can anybody plz explain this to me? What scrip/code is it to start with?

Is it possible to provide an example of how this works? (code)

Dani AI

Generated

As noted, this is handled by the browser History API — that’s the right place to start. Below are practical, modern patterns you can drop into a page: a safe “Back” control with fallbacks, a short note for single‑page apps, and a few troubleshooting/UX points for .

A robust, user-friendly Back button (tries referrer on the same origin, then history, then a final fallback):

<button id="back">Back</button>

<script>
document.getElementById('back').addEventListener('click', function () {
  if (document.referrer) {
    try {
      var ref = new URL(document.referrer);
      if (ref.origin === location.origin) {
        location.href = document.referrer;
        return;
      }
    } catch (e) { /* ignore and fall through */ }
  }

  if (window.history.length > 1) {
    window.history.back();
    return;
  }

  location.replace('/'); // final fallback: home or a safe page
});
</script>

If you build an SPA, push meaningful states so the browser Back works predictably:

history.pushState({page: 'settings'}, '', '/settings');
window.addEventListener('popstate', function (e) {
  // read e.state or location.pathname and render the right UI
});

Notes and gotchas: document.referrer can be empty (direct entry, privacy/referrer policies). history.back() does nothing if there is no previous entry. Avoid hijacking or blocking the native back action — users expect it to work. For accessibility and predictability, always show a visible link that falls back to a known page if history navigation fails.

Recommended Answers

All 2 Replies

In Javascript, you use "(-1)" to go back one page. I haven't seen the "History -1" that you requested.

In Javascript, you use "(-1)" to go back one page. I haven't seen the "History -1" that you requested.

Thank you. The info I received on it was probably incorrect.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.