get back to previous page
Asked Answered
T

7

20

just i want to make back button in my site. once i click the button it need to take the url in to previous page. how can i make this using jquery?

Trouper answered 3/6, 2010 at 17:41 Comment(1)
Could you elaborate on what you are looking for please? It almost seems like you are trying to make the back button goto a new link? I'm not sure.Westward
R
23

the answer by wRAR is correct, you can use history.back or history.go(-1). However, if you are using ajax, you might need a bit more than that.

Stephen Walter has a nice article on how to use jQuery, ASP.NET, and Browser History which basically describes how to save and restore the application state yourself. I recommend you give a read.

Elijah Manor(co-host of the Official jQuery Podcast) has also written a nice article about this topic.

I hope this helps -D

Rachealrachel answered 3/6, 2010 at 18:10 Comment(0)
B
48
<button type="button" onclick="history.back();">Back</button>
Bunco answered 3/6, 2010 at 17:58 Comment(1)
Side note: this is straight Javascript, not jQuery. There are completely history plugins designed using the jQuery plugin framework, but they are meant to handle ajax and your history (to not break the back button on a single page that behaves as many pages).Kurland
R
23

the answer by wRAR is correct, you can use history.back or history.go(-1). However, if you are using ajax, you might need a bit more than that.

Stephen Walter has a nice article on how to use jQuery, ASP.NET, and Browser History which basically describes how to save and restore the application state yourself. I recommend you give a read.

Elijah Manor(co-host of the Official jQuery Podcast) has also written a nice article about this topic.

I hope this helps -D

Rachealrachel answered 3/6, 2010 at 18:10 Comment(0)
A
12

Try this: am using materialise css.

<button id="back_btn" class="btn waves-effect waves-light"  name="action">Back<i class="material-icons left">chevron_left</i>
</button>

In your jquery script file add

$("#back_btn").click(function (){
  window.history.back();
});

within the document ready function.

Atypical answered 29/1, 2018 at 8:51 Comment(0)
T
7

For JQM, this works!

$(document).ready(function(){
    $('.backLink').click(function(){
        parent.history.back();
        return false;
    });
});
Thrombophlebitis answered 13/10, 2015 at 11:2 Comment(0)
W
2

This is what I have successfully used in one of my recent projects:

<script>
    function goBack() {
       window.history.back();
    }
</script>
<a href="#" onclick="goBack()" class="btn-event-show-video">
     Return to Stand
</a>
Wattage answered 2/9, 2020 at 16:5 Comment(0)
P
1

I think button onclick="history.back();" is one way to solve the problem.But it might not work in the following cases:

  1. If the page gets refreshed or reloaded.
  2. If the user opens the link in a new page.

To overcome these, the following code could be used if you know which page you have to return to.

E.g. If you have a no of links on one page and the back button is to be used to return to that page.

<input type="button" onclick="document.location.href='filename';" value="Back" name="button" class="btn">

Pieeyed answered 24/6, 2016 at 12:26 Comment(0)
N
0

I may be a bit late to answer because I went to get milk for my son but here is an improved answer that may help future visitors.

function goBack(){
    if(navigation.canGoBack) {
        history.back();
    }
}

Then you can use the goBack() function on any events, preferably click events.

<a href="#" onclick="goBack()" class="btn-go-back">
    Go back
</a>

We are using the navigation object in the 'if' statement to check if the window can go back in the current website. If we don't do this check, then the users will go back even if the previous page was a different website. In most cases, you don't want your users to go outside of your site or web app.

Nothing answered 5/7, 2023 at 23:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.