Solved

using javascript.history

Posted on 2006-10-31
6
1,424 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.
0
Comment
Question by:fizch
  • 3
  • 3
6 Comments
 
LVL 11

Accepted Solution

by:
mvan01 earned 500 total points
ID: 17844796
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
 
LVL 6

Author Comment

by:fizch
ID: 17844890
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
 
LVL 11

Expert Comment

by:mvan01
ID: 17844917
Doh! ... you're right.   Cookie time?

Peace and joy.  mvan
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 6

Author Comment

by:fizch
ID: 17844940
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
 
LVL 11

Expert Comment

by:mvan01
ID: 17844972
Hi fizch,

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

Peace and joy.  mvan
0
 
LVL 6

Author Comment

by:fizch
ID: 17845108
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

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now