Created
August 12, 2014 15:18
-
-
Save cowboy/60b0820e0cb957a0676b to your computer and use it in GitHub Desktop.
this code from amazon.com's source is... well, just... what web development is all about
This file contains 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
document.write = (function(write){ | |
var override = function() { | |
if(document.readyState !== "loading") { // document has finished loading - we want to intercept this call to document.write | |
if (window.ueLogError) { | |
try { | |
throw new Error("`document.write` called after page load on the gateway."); | |
} | |
catch(e) { | |
ueLogError(e, { logLevel : 'ERROR', attribution: 'gw-third-party-js' }); | |
} | |
} | |
} | |
else if(write.apply) { // modern browsers | |
write.apply(document, Array.prototype.slice.call(arguments, 0)); | |
} | |
else { // old versions of IE | |
document.write = write; | |
var args = ""; | |
for(var a=0; a<arguments.length; a++) { | |
args += (args?",":"")+"arguments["+a+"]"; | |
} | |
eval("document.write("+args+");"); | |
document.write = override; | |
} | |
}; | |
return override; | |
}(document.write)); |
Want to prevent from being overwritten by third-party javascript?
This is their little dirty secret.
Could you explain the purpose of this code ? What is really about ?
@rbecheras if doc.write is called after the page has loaded, the document will be overwritten :/ . This tries to avoid that scenario, e.g. from third party js sources. Check out https://developer.mozilla.org/en-US/docs/Web/API/document.write
Oh lol, one of my clients had this code in their page...
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
(I didn't write this code)