Created
March 7, 2012 15:11
-
-
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
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
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') |
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
<!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