Skip to content

Instantly share code, notes, and snippets.

@danshearmur
Created March 7, 2012 15:11
Show Gist options
  • Save danshearmur/1993718 to your computer and use it in GitHub Desktop.
Save danshearmur/1993718 to your computer and use it in GitHub Desktop.
Replace document.write so that embedding gist script tags doesn't block the page
document._write = document.write;
document.write = function(str){
if (str.indexOf('<div id=\"gist-') != 0) {
if(str.indexOf('https://gist.github.com/stylesheets/gist/embed.css') == -1) {
// if you got this far it's not a github document.write call
document._write(str);
}
return;
}
var id = /<div id=\"gist-(\d+)/.exec(str)[1];
var script = document.querySelector('script[src="https://gist.github.com/' + id + '.js"]');
var div = document.createElement('div');
div.innerHTML = str;
script.parentNode.insertBefore(div, script.nextElementSibling);
}
// * IE8 will have to replace script.nextElementSibling with a
// function to find the next element in the dom
// * IE7 will have to replace querySelector with looping though
// getElementsByTagName('script')
<!doctype html>
<link rel=stylesheet href=https://gist.github.com/stylesheets/gist/embed.css>
<script src=document_write.js></script>
<h1>testing gist:</h1>
<script src="https://gist.github.com/1972983.js"></script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment