Skip to content

Instantly share code, notes, and snippets.

@lealhugui
Last active September 23, 2016 14:27
Show Gist options
  • Save lealhugui/1c473a48cf79d6785af864c0b9cd5f94 to your computer and use it in GitHub Desktop.
Save lealhugui/1c473a48cf79d6785af864c0b9cd5f94 to your computer and use it in GitHub Desktop.
Basic PJAX
<!DOCTYPE html>
<html>
<head>
<title>PJAX exemple</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script type="text/javascript">
function loadData(title, uri){
history.pushState(null, title, uri);
$("#pjax-container").html("<h2>"+title+"</h2>");
overrideA();
}
function overrideA(){
$("a").off().on("click", function(e){
e.preventDefault();
loadData($(this).text(), $(this).attr("href"));
});
}
</script>
<style type="text/css">
html, body {
height: 100%;
width: 100%;
}
</style>
</head>
<body>
<div>
<a href="#/menu1"><h2>Menu 1</h2></a>
<a href="#/menu2"><h2>Menu 2</h2></a>
</div>
<div id="pjax-container">
<h1>Index</h1>
</div>
<script type="text/javascript">
window.onpopstate=function(event){
loadData(document.title, document.location);
};
overrideA();
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment