Skip to content

Instantly share code, notes, and snippets.

@subtleGradient
Created June 21, 2009 22:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • Save subtleGradient/133677 to your computer and use it in GitHub Desktop.
Save subtleGradient/133677 to your computer and use it in GitHub Desktop.
document.write replacement using MooTools
/*
---
source: http://gist.github.com/133677
provides: document.write
description: MooTools based document.write replacement
requires: MooTools
author: Thomas Aylott -- SubtleGradient.com
thanks: Daniel Steigerwald -- daniel.steigerwald.cz
license: MIT
...
*/
(function($){
var wrapper = new Element('div'),
fragment = document.createDocumentFragment();
document._writeOriginal = document.write;
document.write = function(){
var args = arguments, 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);
function documentWrite(){
var html = Array.join(args, '');
document.addEvent('domready', function(){
Array.filter(wrapper.set('html', html).childNodes, document.write.filter).each(function(node){
fragment.appendChild(node);
});
(id = $(id)).parentNode.replaceChild(fragment, id);
});
}
setTimeout(documentWrite, 0);
};
document.write.context = document.body;
document.write.filter = function(el){ return true; };
})(document.id||window.$);
// 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"/>');
@eerne
Copy link

eerne commented Feb 26, 2011

Getting "element is null" on FX 3.6.13 and "Uncaught TypeError: Cannot call method 'appendChild' of null" in Chromium
http://jsfiddle.net/tofu/jEQSH/1/

@subtleGradient
Copy link
Author

Yeah, you can pretty much assume this thing is abandonware at best.
If you (or anyone) does fix it, please link up the fixed version and I'll replace this one.

@eerne
Copy link

eerne commented Mar 1, 2011

@subtleGradient
Seems like replacing $try and $time did the job http://jsfiddle.net/tofu/Ebk6M/

@subtleGradient
Copy link
Author

Oh right, this was written for 1.2.x, it'll need some changes to work with 1.3.x

@eerne
Copy link

eerne commented Mar 1, 2011

@RonnyO
Copy link

RonnyO commented Mar 3, 2011

Just fixed a little bug - If the code was put inside the head section, no context was set as document.body isn't available yet. Moved the initial context setter inside a domready function, as it isn't used anyway before the Browser.loaded flag is up.
This is important because this gist should probably be inside the head to protect any calls later on.
Check out the gist or preview on jsFiddle.

@RonnyO
Copy link

RonnyO commented Mar 3, 2011

oh and any idea why this gist break the twitter widget?
http://jsfiddle.net/Ronny/rj8xf/1/
The doc.write there is pretty basic:
document.write('

').
But still, without this gist the plugin works.

@subtleGradient
Copy link
Author

Looks like they inject something into the DOM and then expect to be able to use it immediately (i.e. before DOMReady).
In that case, there needs to be some way for the document.write replacement to have a whitelist of things that simply cannot function without actually injecting into the DOM at that very instant.

@jameswestgate
Copy link

using to document.write to add the span tag pretty much makes this useless as an approach unfortunately.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment