views:

245

answers:

4

I have been looking around SO for a while and haven't been able to find anything that matches my issue, which I'm not even sure I can explain that well, so take that for what it's worth.

I have a page that loads content into a div via AJAX (using the .load() method). There are several links in the navigation, meaning the content will change while navigating the site without refreshing the entire page.

(Actually, to be honest, I just cribbed the DocTemplate layout [http://css-tricks.com/examples/DocTemplate/] from css-tricks.com. Apparently while I'm not a re-invent the wheel type programmer, I am a bash my head against the wheel incessantly to get it to work programmer.)

So, index.php loads up some DB content in a div. There is also a jQuery UI modal input form on index.php. Essentially, the only HTML on the page is an empty div and a form. This all works fine, until I call up another page, then go back to index.php. The DB content is not loaded, and my form is shown there in all its naked glory. I know why this is happening. The page was not refreshed, nothing kicked off the code to load the content and hide the form.

My question is, how can I ensure that the AJAX .load() and the .dialog() will run when loading index.php again? Is it even possible?

Thanks, and my apologies for the length. I get verbose when I'm confused.

A: 

An easy and quick solution is to disable caching of index.php. You can do that by sending the appropriate Http headers. This way your page will always reload.

kgiannakakis
I'm not sure that will work. I probably should have mentioned that the browser never leaves index.php. Clicking on the nav links loads blah.php into a div on index.php, resulting in a URL that looks like: index.php#blah.php. I tried having the page refresh but it wouldn't load the other pages into the div.
Michael Howland
A: 

Hello,

your question is not very easy to understand. It seems you are using the back button of the browser to go back to your index.php file ?

When you hit the back button, the browser reloads the page as it was originally loaded (that is in the state it was before any user interaction).

So it is normal that the browser shows the naked page.

It you want to handle the back button and more generally the history of the browser in an ajax application, you can use of the existing jquery history plugins, like this one

I hope it will help you,

Jerome Wagner

Jerome WAGNER
Er, yeah, I know it's a little rough. However, I'm not using the back button. I'm clicking nav links to load page content into a div... I'm not sure this is helping.
Michael Howland
A: 

You can add some logic so that when your page has finished loading, you're checking to see whether the dynamic region is populated. If it's not, then run your .load() routine.

$(document).ready( function() {
    if( $('div#theDIV').find('something_that_should_be_there').length == 0 ) {
        $('div#theDIV').load('etc,etc')
    }
})
Ken Redler
The problem comes in after the page is loaded. Main page loads, index.php runs its jQuery fine. I then click on another nav link to load, say, about.php, into the div that was previously occupied by the content from index.php. That works fine, all ajaxy and spiffy-like. Then, I click on the Home nav link to bring back the index.php content - which has the jQuery code. That jQuery doesn't run, since the page never refreshed. Hell, even I'm confusing myself now.
Michael Howland
A: 

I got it working. I have to manually re-bind the events in the .load() call-back function when the content is loaded again.

Michael Howland