EDIT 3 : This jsFiddle works with the plugin, I have almost none.
EDIT 2 : I did fiddle . I cannot get the plugin to work there, but it may be easier to parse.
On the webpage I am creating, articles (posts) and new pages are loaded via Ajax. At the same time, when new Ajax content is loaded, the address bar changes (using the jQuery Address plugin ).
So, normal navigation will look like this:
/ / page2 / Page3 / Article 7 / Page3 / Page4
Please note that the browser button was not pressed in this navigation, and when you exit article 7 , the website returns to its previous URL, which in this case is page3 .
The plugin does not detect when the user pressed the button back or forward. I managed to find a way to find out when the user clicks the Back or Forward button:
Each time I add new content and change the address, I save the new address in an array. If the user presses the back and forward buttons, I analyze it like this:
if (new_url == penultimate_value_in_array) => the back button is pressed the last value from the array)
else => button pressed forward (add a new value to the array)
This works correctly for this kind of navigation:
/ / page2 / Page3 / Page4
In my case, it works great for the back button. However, if the user clicks the Forward button from / article 7 to / page 3, the function will read it Back. Here is an example to understand what I mean:
the user is in / page 4 and presses Back β new_page = / page3, which is equal to perultimate_page_in_array (/ page3) => BACK
/ / page2 / Page3 / Article 7 / Page3 / Page4
the user is in / page 3 and clicks Back β new_page = / article7, equal to perultimate_page_in_array (/ article7) => BACK
/ / page2 / Page3 / & nbsp Article 7; / Page3
the user is in / article 7 and presses Back β new_page = / page3, which is equal to perultimate_page_in_array (/ page3) => BACK
/ / page2 / Page3 / Article 7
the user is in / page 3 and clicks Back β new_page = / page2, equal to perultimate_page_in_array (/ page2) => BACK
/ / page2 / Page3
the user is in / page 2 and presses Forward β new_page = / page3, which is NOT equal to perultimate_page_in_array (/) => FORWARD
// p. 2
the user is in / page 3 and presses Forward β new_page = / article7, which is NOT equal to perultimate_page_in_array (/ page2) => FORWARD
/ / page2 / Page3
the user is in / article 7 and presses Forward β new_page = / page3, which is equal to perultimate_page_in_array (/ page3) => BACK ( , and here the value should be FORWARD )
/ / page2 / Page3 / Article 7
How can I introduce another statement that will determine that False Back should be redirected?
EDIT:
This is the code I'm using, I'm not sure if it will help solve the problem, but here it is:
var site_url = 'www.mysite.com', last_visited_url = '', just_visited_url = $.address.baseURL().replace(site_url,''), visited_pages_array = [just_visited_url], page_number = '';
when visiting a new page: ajax, then:
last_visited_url = just_visited_url; page_number = $('a.next_page_link').attr('href').replace(site_url,''); // /page3 for example $.address.state(site_url).value(page_number); just_visited_url = page_number; visited_pages_array.push(just_visited_url);
when loading an article: ajax, then:
article_url = $('a.article_title').attr('href').replace(site_url,''); // /article7 for example last_visited_url = just_visited_url; $.address.state(site_url).value(article_url); just_visited_url = article_url; visited_pages_array.push(just_visited_url);
if there is an article:
last_visited_url = just_visited_url; $.address.state(site_url).value(page_number);
And this code is executed when the user clicks on some of the browser drills:
$.address.externalChange(function() { var newPage = $.address.baseURL().replace(site_url,''); if (visited_pages_array[visited_pages_array.length-2] == newPage) { visited_pages_array.splice(visited_pages_array.length-1 , 1); console.log('BACK pressed'); } else if (visited_pages_array.length > 1) { visited_pages_array.push(newPage); console.log('FORWARD pressed'); } else { console.log('REFRESH pressed'); } });