Link to home
Start Free TrialLog in
Avatar of fizch
fizch

asked on

using javascript.history

I have got a "back" button that I am using in conjunction with an ASP.Net form. The problem that I have is that any time the page does a post back, it adds another reference to that page in the history. I.E. A user goes to this form, fills out the information clicks the update button. The page then posts back, updates the record, and reloads the page. Now there are two entries for this page in history. I would like for my back button to go back through the history and take me to the first page that is not the current. I have been trying to loop through the history object, but I am not having much luck.

function showHistory()
{
      for(i=0;i<window.history.length; i++)
      {
            alert(window.history[i]);
      }
}

The function that I have listed here just shows "Undefined" in the alert window instead of a url. Can someone please help me with this. It is really being a pain.
ASKER CERTIFIED SOLUTION
Avatar of mvan01
mvan01
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of fizch
fizch

ASKER

I think I understand what you are saying. Everytime the page reloads, add 1 to the counter, then use windows.go(nPages). The only problem that I see with this is getting the value of nPages to transfer from page to page.
Doh! ... you're right.   Cookie time?

Peace and joy.  mvan
Avatar of fizch

ASKER

Ok, help me out with the cookies. I have messed with them a couple of times with server-side code, but never with javascript.
Hi fizch,

Try here for some (simple?) cookie code:  http:Q_20640828.html

Peace and joy.  mvan
Avatar of fizch

ASKER

Ok, I understand where you are headed with this, and I can see the implementation of all of this code. I really don't know that I want to give up the navigation buttons on the browser though. There should be a way for me to keep track of how many times that particular page has loaded and the navigate back n + 1 times. If I do this with a cookie, the variable is no longer controlled by the page, and will definitely get out of whack if the navigation buttons are used.

Perhaps something like this would work. Let me know what you think.

function getLoadCount() {
    var loadCounter;

    try
        loadCounter = parseInt(document.getElementById('txtLoadCount').Text);
    catch (err)
        loadCounter = 0;

    return loadCounter;
}

function page_Load(){
   document.getElementById('txtLoadCount').text = ++getLoadCount();
}

function btnDone_onclick() {
    window.history.go(++getLoadCount());
}


The control 'txtLoadCount' would be a hidden input field.