• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 793
  • Last Modified:

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?
Thanks
intlink.txt
0
avir
Asked:
avir
  • 4
  • 3
2 Solutions
 
leakim971PluritechnicianCommented:
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


http://diveintohtml5.info/history.html
https://developer.mozilla.org/en-US/docs/Web/Guide/API/DOM/Manipulating_the_browser_history

<script type="text/javascript">
$(document).ready(function(){				
	$("a.textLinks").click(function(event) {
		event.preventDefault();	
		event.stopPropagation();
                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 );
		  }
		});	
	});	
});	
</script>

Open in new window

0
 
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.
0
 
leakim971PluritechnicianCommented:
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
avirAuthor Commented:
Okay, I'll give it a try when I get to work tomorrow.
0
 
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

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

		$('a#startLink').click(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 );
		});
		
		$('a#backLink').click(function(){
		    // Get archived content from hidden div
		    var savedData = $('#subjectsTextsSaved').html();
		    // move saved content to main div
		    $('#subjectsTextsDiv').html( savedData );
		    // empty archived div
		    $('#subjectsTextsSaved').html( '' )
		});

	});
</script>

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

<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

0
 
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.
0
 
leakim971PluritechnicianCommented:
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!
0
 
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.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now