Go Back to Previous Page

Go Back to Previous Page

You can use a link to invoke history.go(-1) in Javascript, which is essentially equivalent to clicking the Back button. Ideally, however, it'd be better to just create a link back to the URL from whence the user was posted to the form - that way the proper "flow" of history is preserved and the user doesn't wonder why they have something to click "Forward" to which is actually just submitting the form again.

How to go back to previous page in Next.js using `next/router`?

<Link> can't go outside of your app. (but router.back() can)

You can't use router.back() directly in the code, you need to do something like :

<img onClick={() => router.back()} src="icon.svg" />

<Link> does not have onClick property.

import { useRouter } from 'next/router'

export default function Page() {
const router = useRouter()

return (
<button type="button" onClick={() => router.back()}>
Click here to go back
</button>
)
}

Source of info here

Return to previous window on click of 'Go to previous page' link

Use window.close() to close an opened tab.

Go back to previous page regardless of changes in query string

Instead of history.back(), which goes back only by one, you could do window.location.replace(window.location.host), which loads the root page.

To get more info on window.location: https://developer.mozilla.org/en-US/docs/Web/API/Window/location

Edit:

If you want to ignore URL parameters, you need to change how you go to another page.

Instead of window.history.pushState(...), you should use window.history.replaceState(...), when the navigation should be ignored on a back click.

Here more to read on that topic: What is the trade off between history push and replace?

Return To Previous Page - Laravel 8 Project

This was very useful Laravel Session

  public function index(Request $request){
$categories = Category::latest()->paginate(5);
//the following snippet creates persistent session variables
$request->session()->put('page',$categories->currentPage());
return view('orders.view',compact('orders'));
}

Also changed the View Categories button from this:

<a href="{{ url()->previous() }}" class="btn btn-primary"><i class="material-icons">arrow_back</i> View Categories</a>

To this:

<a href="{{ Session::has('page') ? '/categories?page=' . Session::get('page') : '/categories' }}" class="btn btn-primary"><i class="material-icons">arrow_back</i> View Categories</a>

This button first tests if $_SESSION['page'] was set and if so when clicked it assigned the last known value to ?page=, as extra precaution, if for whatever reason the session value was not found the button will just redirect back to category.view which is the categories landing page.

This is exactly what I wanted to achieve and I was surprised to find that nothing native or built-in method exists because I've checked most public forums and re-checked the Laravel Doc and found nothing to what I'm looking for.
Maybe the question was just not clear enough but I am sure I've provided enough material to give a clear picture of what I wanted to achieve.



Related Topics



Leave a reply



Submit