www.webdeveloper.com
Results 1 to 7 of 7

Thread: Keeping track of history and simulating forward and backward buttons

  1. #1
    Join Date
    Dec 2005
    Posts
    268

    Keeping track of history and simulating forward and backward buttons

    Hi,

    First of all, I am not trying to use the browsers forward and backwards button. What I am trying to do is add a forward and backward button to a dialog.

    The dialog will have some links which contain the page ID instead of a real href, and if clicked will use ajax to return the content for that given page with potentially more links.

    I am struggling on how to keep track of the history. I suppose that every time I click a link, I must push the id to an array. If I go backwards, I can't remove the page ID because it will still be needed for the forward button. But if going back, and then forward up a different tree, I need to delete the page IDs up the no longer current tree.

    I hope I am making at least some sense, and believe I am not the first person to try to do this. Any recommendations on how to accomplish it?

    Thank you

  2. #2
    Join Date
    May 2006
    Location
    Somewhere behind your screen
    Posts
    1,671
    if php is available you can store|update|delete everything in $_SESSION

  3. #3
    Join Date
    Nov 2013
    Location
    London, United Kingdom
    Posts
    15
    I'm assuming that you want to use JavaScript for that. What you have described can be done in numerous ways but the model will be the same:

    Code:
    var pages = [ ];
    var currentPage = -1;
    
    function navigate(page) {
      pages.splice(currentPage + 1);
      pages.push(page);
      currentPage += 1;
    }
    
    function back() {
      currentPage -= 1;
    }
    
    function forward() {
      currentPage += 1;
    }
    You can think about wrapping this model into the class and create a few another useful methods, like isBackPossible.

  4. #4
    Join Date
    Dec 2005
    Posts
    268
    Thank you AlexAtNet, Very impressive.

    isBackPossible would return true if currentPage is 0 or greater, correct?

    isFowardPossible would return true if pages.length-currentPage is 2 or greater, correct?

    Any other desired methods that I am not thinking about?

    While it works, why start currentPage at -1 and not 0?

    Thanks again!

  5. #5
    Join Date
    Nov 2013
    Location
    London, United Kingdom
    Posts
    15
    Quote Originally Posted by NotionCommotion View Post
    isBackPossible would return true if currentPage is 0 or greater, correct?
    currentPage is 0 if it is on the first item in the history so the back is not possible.

    Quote Originally Posted by NotionCommotion View Post
    isFowardPossible would return true if pages.length-currentPage is 2 or greater, correct?
    isFowardPossible should return true if currentPage < page.length - 1. page.length - 1 should be maximum value for the currentPage and it means that currentPage is points to the top of the history.

    Quote Originally Posted by NotionCommotion View Post
    Any other desired methods that I am not thinking about?
    this highly depend on the further requirements to the component

    Quote Originally Posted by NotionCommotion View Post
    While it works, why start currentPage at -1 and not 0?
    by definition, currentPage points to the element in the list, but when the list is empty any currentPage value will be incorrect so -1 is just a handy value that looks incorrect and it is fine

  6. #6
    Join Date
    Nov 2013
    Location
    London, United Kingdom
    Posts
    15
    BTW, if you are learning JavaScript this article can be useful: http://alexatnet.com/articles/model-...mvc-javascript

  7. #7
    Join Date
    Dec 2005
    Posts
    268
    Quote Originally Posted by AlexAtNet View Post
    currentPage is 0 if it is on the first item in the history so the back is not possible.
    In regards to isBackPossible, I open up a new dialog and currentPage is set -1. I click a link, and currentPage is now equal to 0. I should be able to go back, no? What am I missing?

    Quote Originally Posted by AlexAtNet View Post
    this highly depend on the further requirements to the component
    Suppose so, but just making sure there wasn't any obvious ones which I will only realize I missed when I am done.

    Quote Originally Posted by AlexAtNet View Post
    BTW, if you are learning JavaScript this article can be useful: http://alexatnet.com/articles/model-...mvc-javascript
    I use MVC for PHP applications, but never JS. I will check out the article.

    Thanks again!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles