jquery load previous content

I am loading some html content into the same div using a text link. Is there a way of going back to the previous content once the new content is loaded. Clearly the back button doesn't work, but can I somehow add a link to the new text that will return me to the previous content?
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

You may use pushState so you can use the browser back button or link doing the same thing :
<a href="javascript:history.back();">previous</a>

Open in new window


<script type="text/javascript">
	$("a.textLinks").click(function(event) {
                var url = '/human-sanctuary/assets/content/texts/' + event.target.id + '.html';
history.pushState({}, "", url);
		$( "#textDiv").load(url, function( response, status, xhr ) {
		  if ( status == "error" ) {
			var msg = "Sorry but there was an error: ";
			$( "#error" ).html( msg + xhr.status + " " + xhr.statusText );

Open in new window

avirAuthor Commented:
Thanks for your reply.
Unfortunately it's not really working.
For instance, if you go to
http://www.imj.org.il/human-sanctuary/subjects/show and click on the Settlement and Caves link on the right and then click on the Ritual Baths link in the text, I would like the viewer to be able to get back to the Settlement and Caves text using a back link. I added that link at the bottom of the Ritual Baths text and your corrected script but it goes back to the same text.
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

avirAuthor Commented:
Okay, I'll give it a try when I get to work tomorrow.
MurfurFull Stack DeveloperCommented:
Just as you can load content into a selected element, so you can also capture its contents. Not difficult and an uncomplicated approach would be to have another div on the page that is hidden and simply transfer the existing content to that div just before you load the new content in your AJAX call. Then your back link triggers the reversal of that operation, replacing the current content with the saved old content

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
	$(document).ready(function() {

			// Get previous content
			var oldData = $('#subjectsTextsDiv').html();
			var newData = '<h3>Text block 2</h3><p>Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt.</p>';
			// save previous content to hidden div
			$('#subjectsTextsSaved').html( oldData );
			// load new content
			$('#subjectsTextsDiv').html( newData );
		    // Get archived content from hidden div
		    var savedData = $('#subjectsTextsSaved').html();
		    // move saved content to main div
		    $('#subjectsTextsDiv').html( savedData );
		    // empty archived div
		    $('#subjectsTextsSaved').html( '' )


<a id="startLink" href="#">Start</a>&nbsp;&nbsp;<a id="backLink" href="#">Back</a>

<div id="subjectsTextsDiv" style="display: inline-block; margin: 10px; padding: 5px; border: 1px solid blue; height: auto; font-family: Verdana; color: #333;"><h3>Text block 1</h3><p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p></div>
<div id="subjectsTextsSaved" style="display: inline-block; margin: 10px; padding: 5px; border: 1px solid red; height: auto;"></div>

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
avirAuthor Commented:
Thanks for the idea. I got a little side tracked with other projects, but I'll have to try it. The other suggestion, which seems to be viable, wasn't really working for me, I think because of the way my code is placed on the pages.
if you're afraid or have issue  to manage this yourself, you can use a jQuery plugin : http://plugins.jquery.com/tag/pushstate/

Kool Swap
Kool Swap enables impressively ajax page load support with history for websites. Just tell the plugin what should be switched and swap pages in a nice way!
avirAuthor Commented:
I've tried both solutions by themselves and they work great, but I think I'm going to need some more time to figure out what works best on the site and it might be a while until I get back to working on it. Thanks for both your help.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

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.