We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

using javascript.history

fizch
fizch asked
on
Medium Priority
1,577 Views
Last Modified: 2012-06-27
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.
Comment
Watch Question

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

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

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.

Commented:
Doh! ... you're right.   Cookie time?

Peace and joy.  mvan

Author

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.

Commented:
Hi fizch,

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

Peace and joy.  mvan

Author

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.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.