Created
February 3, 2014 20:04
-
-
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.
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
(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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.