2

I have searched high and low for this but I can't seem to find anything for my specific use-case.

I want to detect when user clicks the browser's back button and do something. Specifically, when a user clicks the back button another element on the page will be clicked instead of actually going back to the previous page.

EDIT: This is not a duplicate. I am aware of that thread and we have different use-case. I don't think that will work for me. jQuery address only works for browser history so that when the back button is clicked it will load the previous content in ajax. Perfect for the former's use-case. However mine is different.

I have a simple text page.

<div>
    <p> foo foo foo foo foo foo foo </p>
     <a href="" id="1"> </a>
     <a href="" id="2"> </a>
     <a href="" id="3"> </a>
</div>

Now when the browser's back button is clicked I want to user to redirect to id 3 instead of going back to the previous page.

Unless I'm missing something here? How can I use jQuery address to achieve what I want?

Furthermore, I found this. Browser Back Button Detection Where you could do something like this when you use his backfix.min.js:

   <script type="text/javascript" src="backfix.min.js"></script>
   <script type="text/javascript">
   bajb_backdetect.OnBack = function()
   {
   alert('You clicked it!');
   }
   </script>

Working as it should but I can't seem to make it work in my case. I'm trying to do the following but it just doesn't work:

    <script type="text/javascript">
    bajb_backdetect.OnBack = $(function() {
        $("a#3").trigger('click');
    });
    </script>

This is probably where I'm messing up. So I need your help here.

Nonetheless. I am still open to any possible solution. Thanks.

joejam
  • 265
  • 2
  • 6
  • 8
    Don't mess with user's control over his browser.. – enapupe Mar 21 '14 at 16:13
  • 2
    This question appears to be off-topic because you are attempting to make the web horrible. – Digital Chris Mar 21 '14 at 16:21
  • Take a look at this link. It explains a workaround based on the anchor part of the URL: http://www.webdesignerdepot.com/2013/03/how-to-manage-the-back-button-with-javascript/ - even though the explanations are a bit confusing to me, but the general concept is using anchors to stay on the same page and preserve the JavaScript state. Upon clicking links the anchor part will be updated (and therefore also the browser history). You need to detect the changing URL and respond to the changing anchor string. And the great thing is, this does not mess around with the browser default behavior. – tiguchi Mar 21 '14 at 16:25
  • @DigitalChris Well my intentions are good. Nonetheless my question still stands. How? – joejam Mar 21 '14 at 21:03
  • @NobuGames thanks for that. The solution on that page is somehow similar to that "duplicate thread". – joejam Mar 21 '14 at 21:05
  • How does this duplicate thing work. How do you remove this.... – joejam Mar 21 '14 at 21:13

0 Answers0