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.
LVL 6
fizchAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
mvan01Connect With a Mentor Commented:
Hi fizch,

Due to security reasons, you cannot review the entries of the history object.  (Would it be 'secure' to let any random website to which you surf dump the entire contents of where you've surfed since you opened your browser?  I don't think so.)

You can, however use:  history.go(n); where n is how many pages to go back.

So, create a global JavaScript variable by declaring it outside of any functions.  Set it initially to 1 (e.g.: var nPages=1;).  When you submit, first increment the variable, so that it will be set to 2 (e.g: onclick="nPages++;doOtherThings;").  Include a message on your page to not use the browser back button, but include one on your page, to go back the correct number of pages (e.g.: onclick="history.go(nPages);".

Finally, when you submit and the page reloads, you don't want to reinitialize the variable to 1, so instead of:
var nPage=1;
you need:
if(document.referrer!=document.location.href) var nPages=1;

Note that if the user doesn't heed your message and instead clicks the browser's back button, the page appears to go nowhere, so they'll probably want to follow your directions.  (Of course, then if they do, they'll be going back 2 from the first page, and maybe be sent too far???)

Peace and joy.  mvan
0
 
fizchAuthor Commented:
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.
0
 
mvan01Commented:
Doh! ... you're right.   Cookie time?

Peace and joy.  mvan
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

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

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

Peace and joy.  mvan
0
 
fizchAuthor Commented:
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.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.