Forum Moderators: open

Message Too Old, No Replies

How to trigger a js function with the browser back button?

         

keitai

3:20 pm on Mar 17, 2010 (gmt 0)

10+ Year Member



Hi,

I a bit stuck with jquery/ajax and the browser back button.
Summary: i have a multiple search filter page, where the results are shown in an ajax div. Now each result row is clickable and a detailed page is shown.
On this detailed page i have a go back link which finally works. The back link triggers a certain .js function, i would like to trigger this js function with the browser back button as well. Is this possible?

regards

rainborick

3:28 pm on Mar 17, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You can't specifically intercept a user pressing the Back button. At best, you can add an onunload() function to the <body> tag.

I suspect that your situation will require tracking the user's path through your site so that when each page loads, you can tell which page from your site (if any) the user last visited.

keitai

4:06 pm on Mar 17, 2010 (gmt 0)

10+ Year Member



hmm, your suggestion sounds good. But in my case i don't know how i would accomplish that. I was looking into the "anchor or hash navigation" solution but i couldn't make it work.

If you have the time here's my current simplyfied setup:
SNIP

Any suggestions how to fix the back link issue is appreciated.

regards

[edited by: Fotiman at 5:16 pm (utc) on Mar 17, 2010]
[edit reason] No URLs please. See TOS [webmasterworld.com] [/edit]

rainborick

4:14 pm on Mar 17, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



If your site is all PHP, check out SESSIONS which you can use to hold user data.

keitai

4:52 pm on Mar 17, 2010 (gmt 0)

10+ Year Member



i can't see why php sessions could help me with the Ajax-back issue/problem. could you point me in the right direction?/

rainborick

6:09 pm on Mar 17, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You need a mechanism to store the history of the user's visit to your site so that you can tell at any moment which page he last visited from your site. Sessions is an easy mechanism to hold such data when you're working with PHP. You could, for example, use PHP to generate the JavaScript that would pass this information to the rest of your code. Or you could use AJAX to call a PHP script to retrieve this information. You could also use cookies, but they can be a problem since some users block all cookies.

keitai

8:25 pm on Mar 18, 2010 (gmt 0)

10+ Year Member



hmm, i am really struggle with this. What seemed to be simple is becoming a major headache ;)

here's a minimal ajax enabled
<snipped url>

but i want it to be ajax enable with the back button for this i am trying BBQ (<snipped url>)

URL'S REMOVED

But i can't get back to a resulting page after filters have been selected?

[edited by: Fotiman at 12:50 pm (utc) on Mar 22, 2010]

[edited by: whoisgregg at 5:04 pm (utc) on Mar 22, 2010]
[edit reason] Whoops, no URLs please. See TOS [webmasterworld.com] :) [/edit]

adni18

6:31 pm on Mar 20, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Try putting an invisible iframe called Page B in your (main) Page A. When Page A loads, the iframe goes from page B to page C. That way, a back button click will cause page C to change back to page B. You can monitor the location.href of the iframe with javascript and call your function when it changes to page B, then reset it to page C if you like.