Skip to content

Instantly share code, notes, and snippets.

@ernestlv
Created February 3, 2014 20:04
Show Gist options
  • Save ernestlv/8791337 to your computer and use it in GitHub Desktop.
Save ernestlv/8791337 to your computer and use it in GitHub Desktop.
document.write cannot be invoked after DomContentLoaded event; Since it will wipe out its content. This code rewrites document.write and allows you to inject HTML code at any time in the page.
(function($){
$.fn.writeFrom = function(callback){
var $that = this;
setTimeout(function(){
document.write = function(){
try{
$.fn.append.apply($that, arguments);
}catch(e){
console.log('INVALID APPEND', e.message);
};
};
callback && callback();
}, 0);
};
})(jQuery);
@ernestlv
Copy link
Author

ernestlv commented Feb 3, 2014

the way you invoke the code is:

$("#myAd").writeFrom( loadMyAd );

where:

myAd: is an element where ad content will be injected.

loadMyAd: is a JS function that will eventually call document.write to inject ad content.

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