0

I would like to navigate to previous page when I click back button. But when I click back button it redirects me to same page since my page has so many ajax calls.

Is there any javascript code available to navigate to previous page?

I have tried below code in my cshtml page.

<input id="btnback" type="button" value="Back" onclick="GoBack();" />

<script type="text/javascript" language="javascript">
    function GoBack() {
        window.history.back()
    }
</script>

<script type="text/javascript" language="javascript">
        function GoBack() {
            window.history.go(-1)
        }
</script>
Tech Learner
  • 1,227
  • 6
  • 24
  • 59
  • In goback() , redirect to hard coded URL. – Tech Learner Sep 27 '17 at 06:45
  • @G01 - If it is a single page then your suggestion might work but I have many pages. I believe hard coding won't suit my requirement here. – Tech Learner Sep 27 '17 at 06:47
  • Similar issue: https://stackoverflow.com/questions/18465895/on-html-actionlink-click-go-to-previous-page. You can use either `ActionLink` with `href` attribute or `window.location.href` with `Request.UrlReferrer` to go back, but be aware if the previous page contains query string to process certain action. – Tetsuya Yamamoto Sep 27 '17 at 06:54
  • agree with @TetsuyaYamamoto I was reading that post too probably best design implementation for you – rmjoia Sep 27 '17 at 06:55
  • @rmjoia - Given link redirecting to homepage but my requirement is go back to previous page. i.e., if current page and previous page URLs are same then that is not my previous page. – Tech Learner Sep 27 '17 at 06:58
  • but in that link I think you have an example of finding out the referrer and then go back to that page, that was my point. – rmjoia Sep 27 '17 at 06:59
  • well, just hitting F12 and typing that code in the console works.. do you get any error in the console when you run your code? – rmjoia Sep 27 '17 at 07:01
  • Both `window.history.back()` & `window.history.go(-1)` works in FF & Chrome... are you missing something? Can you provide which HTML element bounds to `GoBack` function? – Tetsuya Yamamoto Sep 27 '17 at 07:05
  • @rmjoia - Which code you are referring to. I am confused between two links shared in the comments. – Tech Learner Sep 27 '17 at 07:08
  • @TetsuyaYamamoto - Which code you are referring to. I am confused between two links shared in the comments. – Tech Learner Sep 27 '17 at 07:08
  • @TetsuyaYamamoto - In button OnClick I need to write logic. – Tech Learner Sep 27 '17 at 07:10
  • @TetsuyaYamamoto window.history.back() & window.history.go(-1) both works if my current page has no ajax call but one of my page has ajax call. So it treats same url as my previous page. i.e., if current page and previous page URLs are same then that is not my previous page. – Tech Learner Sep 27 '17 at 07:13
  • oh.. then you should track the referrer from the ajax call and then use it to navigate back, the javascript funcion works with the address navigation, so, of course, if the address doesnt change, it wont work. I would look that property or store the navigation in some kind of stack, so the user can always go back.hope it helps @Ask_SO. – rmjoia Sep 27 '17 at 07:21
  • @ASK_SO AFAIK if AJAX for pagination or something like that used, these issues are similar: https://stackoverflow.com/questions/5943195/how-to-make-the-browser-back-button-take-you-back-through-ajax-calls & https://stackoverflow.com/questions/28747706/mvc-ajax-form-and-browser-back-button. If the previous page is a login page, of course you don't want to re-authenticate with current session. – Tetsuya Yamamoto Sep 27 '17 at 07:26
  • @TetsuyaYamamoto is that possible to use URLReferrer in button OnClick. Can you please share me a link if possible. – Tech Learner Sep 27 '17 at 07:30
  • `window.location.href = '@Request.UrlReferrer'` => this redirects page to the referrer URL - ensure you have `onclick` event: ``. Note that `Request.UrlReferrer` may be empty if no previous URL present. – Tetsuya Yamamoto Sep 27 '17 at 07:39

2 Answers2

2

You can do this in your html instead of writing function separately in scripts. See below :-

<input action="action" onclick="window.history.go(-1); return false;" type="button" value="Back" />

You can see my code in screenshot. Go Back

I tried it and it is working. I hope this works for you as well :-)

Thanks.

Archana Parmar
  • 558
  • 3
  • 13
0

Could you just add return false; to your existing javascript function and then check