Skip to content

Instantly share code, notes, and snippets.

@cmsd2
Created May 27, 2011 11:33
Show Gist options
  • Save cmsd2/995079 to your computer and use it in GitHub Desktop.
Save cmsd2/995079 to your computer and use it in GitHub Desktop.
document.write replacement for jQuery
/*
---
source: http://gist.github.com/995079
based on script for MooTools: http://gist.github.com/133677
provides: document.write
description: jQuery based document.write replacement
requires: jQuery
author: Chris Dawes -- [email protected]
thanks: Thomas Aylott -- SubtleGradient.com
thanks: Daniel Steigerwald -- daniel.steigerwald.cz
license: MIT
...
*/
define(function(require, exports, module) {
var $ = require("jquery");
var me = {};
me.wrapper = $('<div></div>');
me.fragment = document.createDocumentFragment();
me.browser_loaded = false;
me.join = function(list, sep) {
return _.reduce(list, function(accumulator, item) { return accumulator + sep + item; });
};
$(document).ready(function() {
me.browser_loaded = true;
});
document._writeOriginal = document.write;
document.write = function() {
var args = arguments,
id = 'document_write' + (new Date().getTime());
if (!me.browser_loaded) {
document._writeOriginal('<span id="' + id + '"></span>');
} else {
$(document.write.context).append($('<span></span>').attr('id', id));
}
function documentWrite(){
var html = me.join(args, '');
$(document).ready(function(){
me.wrapper.html(html);
me.wrapper.children().each(function() {
var node = this;
me.fragment.appendChild(node);
});
var place_holder_el = $('#' + id);
place_holder_el.replaceWith(me.fragment);
});
}
setTimeout(documentWrite, 0);
};
document.write.context = document.body;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment