Skip to content

Instantly share code, notes, and snippets.

@GerHobbelt
Forked from mbostock/.block
Created April 27, 2012 00:35
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save GerHobbelt/2504633 to your computer and use it in GitHub Desktop.
Save GerHobbelt/2504633 to your computer and use it in GitHub Desktop.
D3 PJAX (& execute JS loaded through the d3.html() request as well)
# Editor backup files
*.bak
*~
<!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>
<p>This section was generated at <script type="text/javascript" id="a1">
/* document.write("[A]: " + new Date()); */
d3.select('#a1').replaceWtext("[A]: " + new Date());
console.log("A");
</script>.</p>
<p>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.</p>
</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>
<p>This section was generated at <script type="text/javascript" id="b1">
/* document.write("[B]: " + new Date()); */
d3.select('#b1').replaceWtext("[B]: " + new Date());
console.log("B");
</script>.</p>
<p>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.</p>
</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>
<p>This section was generated at <script type="text/javascript" id="c1">
/* document.write("[C]: " + new Date()); */
d3.select('#c1').replaceWtext("[C]: " + new Date());
console.log("C");
</script>.</p>
<p>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.</p>
</div>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
This page was generated at <script type="text/javascript">document.write(new Date()); console.log("main");</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 type="text/javascript" src="http://mbostock.github.com/d3/d3.js?2.5.0"></script>
<script type="text/javascript">
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);
d3.select(content).selectAll('script').each(function() {
evalScript(this);
});
pjax(links, content); // reapply
});
}
d3.select(window).on("popstate", function() {
if (d3.event.state) load(d3.event.state);
});
}
var evalScript = function(node){
var p, f, id, n = document.createElement("script");
n.type = "text/javascript";
n.text = node.innerText;
f = node.getAttribute('src');
if (f) n.setAttribute('src', f);
id = node.getAttribute('id');
if (id) n.setAttribute('id', id);
p = node.parentNode;
p.replaceChild(n, node);
};
// Hacky Becky to fake/replace document.write() which will screw you over when you're doing it in XHR loaded+eval()ed code.
d3.selection.prototype.replaceWtext = function(txt) {
var o = this.node(), p, id, n = document.createElement("span");
n.textContent = txt;
id = o.getAttribute('id');
if (id) n.setAttribute('id', id);
p = o.parentNode;
p.replaceChild(n, o);
};
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment