Skip to content

Instantly share code, notes, and snippets.

@runeb
Created January 30, 2012 15:24
Show Gist options
  • Save runeb/1704957 to your computer and use it in GitHub Desktop.
Save runeb/1704957 to your computer and use it in GitHub Desktop.
document.write ad voodoo
function loadAd(url, div) {
function makeAdParser(el, cb) {
var inNoScriptTag = false;
var parser = function(txt) {
txt = txt.trim();
if(txt === '</noscript>') { inNoScriptTag = false; return; }
if(txt === '<noscript>') { inNoScriptTag = true; return; }
if(inNoScriptTag) { return; }
if(txt.indexOf('<scr') === 0) {
// Load the script
$('body').append(txt);
} else if(txt.indexOf('<a') === 0) {
// Hey look, its the AD "finally"
// Append it to the div
div.html(txt);
// Report back home
cb();
}
};
return parser;
}
var dw = document.write;
document.write = makeAdParser(div, function() { document.write = dw; });
var adScript = $('<script/>').attr('src', url);
$('body').append(adScript);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment