Created
May 28, 2013 09:09
-
-
Save dehart/5661521 to your computer and use it in GitHub Desktop.
Bookmarkable pages that are loaded by ajax
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<a href='#t-rex' onclick='return getDinoInfo("t-rex");'>T-Rex</a><br /> | |
<a href='#brachiosaurus' onclick='return getDinoInfo("brachiosaurus");'>Brachiosaurus</a><br /> | |
<a href='#pterodactylus' onclick='return getDinoInfo("pterodactylus");'>Pterodactylus</a> | |
<div id='dino-details'></div> | |
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script> | |
<script type='text/javacript'> | |
$(document).ready(function() { | |
//makes sure that the window.location.hash exists | |
//and is returning a nice string | |
if (typeof(window.location.hash) == "string") { | |
//get rid of the # on the string | |
var hash_result = window.location.hash.replace('#', ''); | |
//we have some data after the hash to work with! yay! | |
if (hash_result != "") { | |
getDinoInfo(hash_result); | |
} | |
} | |
}); | |
function getDinoInfo(dino_id) | |
{ | |
if (dino_id != "") { | |
//set the hash on the window to our new value | |
window.location.hash = "#" + dino_id; | |
//makes the Ajax call | |
$.getJSON("/get_dino_info.php", { id: dino_id }, function(data) { | |
//put the results on the page! | |
$("#dino-details").html(data.info); | |
}); | |
} | |
//returning false here allow to call the function without | |
//following the 'href' property on the 'a' tags | |
return false; | |
} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment