Created
January 30, 2012 15:24
-
-
Save runeb/1704957 to your computer and use it in GitHub Desktop.
document.write ad voodoo
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 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