-
-
Save digitarald/308739 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/* | |
--- | |
source: http://gist.github.com/133677 | |
provides: document.write | |
description: MooTools based document.write replacement | |
requires: MooTools | |
author: Harald Kirschner -- digitarald.de | |
author: Thomas Aylott -- SubtleGradient.com | |
thanks: Daniel Steigerwald -- daniel.steigerwald.cz | |
license: MIT | |
... | |
*/ | |
(function($){ | |
var wrapper = new Element('div'), | |
fragment = document.createDocumentFragment(), buffer; | |
document._writeOriginal = document.write; | |
document.write = function(){ | |
var html = arguments; | |
if (document.write.buffer) { // flush multiple write() as one | |
if (!buffer) { | |
buffer = []; | |
document.write.delay(13, document, []); // trigger flush with empty argument | |
} | |
buffer.extend(html); | |
if (html.length) return; | |
html = buffer; | |
buffer = null; | |
} | |
var id = 'document_write' + $time().toString(36); | |
if (!Browser.loaded) document._writeOriginal('<span id="' + id + '"></span>'); | |
else id = new Element('span',{id: id}).inject(document.write.context || document.body); | |
function documentWrite(){ | |
document.addEvent('domready', function(){ | |
Array.filter(wrapper.empty().set('html', Array.join(html, '')).childNodes, document.write.filter || $lambda(true)).each(function(node){ | |
fragment.appendChild(node); | |
}); | |
(id = $(id)).parentNode.replaceChild(fragment, id); | |
}); | |
} | |
setTimeout(documentWrite, 0); | |
}; | |
})(document.id || window.$); |
This file contains hidden or 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
// USAGE EXAMPLES // | |
var start = +new Date; | |
var i = 1000; while (i--) { | |
document.write(i+' '); | |
} | |
var end = +new Date; | |
alert(end-start); | |
window.addEvent('domready',function(){ | |
document.write('Lorem ipsum dolor sit amet'); | |
var myDiv = new Element('div'); | |
document.write.context = myDiv; | |
document.write('Lorem ipsum dolor sit amet'); | |
myDiv.inject(document.body,'top'); | |
}); | |
document.write(1,2,3,4); // handles multiple arguments | |
document.write(); // doesn't break | |
document.write(null); | |
// Add a filter to stop certain things from being injected into your page | |
document.write.filter = function(el){ | |
return el && !$try(function(){ return el.get('tag') == 'link' }) | |
}; | |
document.write('<link rel="stylesheet" href="http://gist.github.com/stylesheets/gist/embed.css"/>'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment