Skip to content

Instantly share code, notes, and snippets.

@mbostock mbostock/.block
Last active Feb 9, 2016

Embed
What would you like to do?
D3 PJAX
license: gpl-3.0
<!DOCTYPE html>
<html>
<body>
<div id="main">
<ul>
<li><b>Anteater</b></li>
<li><a href="b.html">Baobao</a></li>
<li><a href="c.html">Cuttlefish</a></li>
</ul>
Anteaters, also known as antbear, are the four mammal species of the suborder Vermilingua (meaning "worm tongue") commonly known for eating ants and termites. Together with the sloths, they compose the order Pilosa. The name "anteater" is also colloquially applied to the unrelated aardvark, numbat, echidna, and pangolin.
</div>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<div id="main">
<ul>
<li><a href="a.html">Anteater</a></li>
<li><b>Baobab</b></li>
<li><a href="c.html">Cuttlefish</a></li>
</ul>
Adansonia is a genus of eight species of tree, six native to Madagascar, one native to mainland Africa and the Arabian Peninsula and one to Australia. The mainland African species also occurs on Madagascar, but it is not a native of that island. A typical common name is baobab. Other common names include boab, boaboa, bottle tree, upside-down tree, and monkey bread tree. The generic name honours Michel Adanson, the French naturalist and explorer who described A. digitata.
</div>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<div id="main">
<ul>
<li><a href="a.html">Anteater</a></li>
<li><a href="b.html">Baobao</a></li>
<li><b>Cuttlefish</b></li>
</ul>
Cuttlefish are marine animals of the order Sepiida. They belong to the class Cephalopoda (which also includes squid, octopuses, and nautiluses). Despite their name, cuttlefish are not fish but molluscs.
</div>
</body>
</html>
<!DOCTYPE html>
<meta charset="utf-8">
This page was generated at <script>document.write(new Date)</script>.
<div id="main">
<ul>
<li><a href="a.html">Anteater</a></li>
<li><a href="b.html">Baobao</a></li>
<li><a href="c.html">Cuttlefish</a></li>
</ul>
Hello, PJAX!
</div>
<script src="//d3js.org/d3.v3.min.js"></script>
<script>
pjax("ul li a", "#main");
function pjax(links, content) {
d3.selectAll(links).on("click", function() {
history.pushState(this.href, this.textContent, this.href);
load(this.href);
d3.event.preventDefault();
});
function load(href) {
d3.html(href, function(fragment) {
var target = d3.select(content).node();
target.parentNode.replaceChild(d3.select(fragment).select(content).node(), target);
pjax(links, content); // reapply
});
}
d3.select(window).on("popstate", function() {
if (d3.event.state) load(d3.event.state);
});
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.