views:

82

answers:

4

Here is my what I have

<div id=A></div>
<div id=B></div>
<input type="button" value="ChangeA" onClick="createTableA();">
<input type="button" value="ChangeB" onClick="createTableB();">

So in my jsp file, I use javascript and jQuery to manipulate the content of those two div dynamically. For example, if I click on changeA, the function createTableA() will dynamically manipulate <div id=A></div> and append a table to it. So my question is if I click on changeA, then click changeB, how can I manipulate the history so that if I click the back button, I go back to the content of Table A

+1  A: 

I've been using the jQuery History plugin for just this sort of thing and it's been working pretty well for me.

Each "page" is referenced by a hash in your URL. That way "changing pages" doesn't refresh the page, but does store the page state in history and allow for bookmarking.

EDIT I'll expand on the example given in the link to apply more for your situation.

function loadTable(hash)
{
    if(hash == "ChangeA")
        createTableA();
    if(hash == "ChangeB")
        createTableB();
}
$(document).ready(function() {
    $.history.init(loadTable);
    $("input[id^='Change']").click(function(){
        $.history.load(this.attr('value'));
        return false;
    });
});

What the above code does is sets an event handler on all input tags whose id begins with 'Change' so that when those buttons are clicked, loadTable is called. If you change your buttons to look like this:

<input type="button" id="ChangeA" value="ChangeA">
<input type="button" id="ChangeB" value="ChangeB">

clicking button A will put this http://www.example.com/yourpage.html#ChangeA in the address bar and load table A, also adding that table change to the browser history.

Aaron
U think u can give me a quick example code. The example on the website does not make a lot of sense to me.
Harry Pham
Added an example. Hopefully that helps.
Aaron
I successfully implement this. However, I dont think it is quite what I fully want. This does not retain the content of the page, it just recall the function(). I run into problem of passing parameter to the function...like let say in `createTableA()`, there is a `checkbox`, and the content B of it depend of what `checkbox` I am clicking. So when I `check` box, click `ChangeB`, content of B now load. So when I click the `back` button, content of A reload (good good !), but then if I click `forward`, the content of B does not reload correctly, since it does not know which `checkbox` I click.
Harry Pham
A: 

The native 'location' object has a 'hash' property that you could use for navigation in AJAX/JS applications.

You could use History plugin or Address plugin.

Address plugin gives more flexibility and recommended for more complex apps.

shazmo
I try history plug in, and I run into problem of passing parameter to the function...like let say in `createTableA()`, there is a checkbox, and the content B of it depend of what checkbox I am clicking. So when I check box, click ChangeB, content of B now load. So when I click the back button, content of A reload (good good !), but then if I click forward, the content of B does not reload correctly, since it does not know which checkbox I click. I am trying to look at Address Plugin now, but do u know on top of your head, that does address plugin can solve my problem?
Harry Pham
The Address plugin is little different from History plugin. In your case you need to set the url parameters in createTableB function. In the externalChange callback get those url hash parameters using event.parameters. http://www.asual.com/jquery/address/samples/form/
shazmo
A: 

You should check out Ben Alman's Back Button and Query Library Great api for mucking with the browser history and has some great examples to get you started.

PetersenDidIt
A: 

YUI also has a browser history manager: YUI3: http://developer.yahoo.com/yui/3/history/ or YUI 2: http://developer.yahoo.com/yui/history/

ysaw