Skip to content

Instantly share code, notes, and snippets.

@cowboy
Created August 12, 2014 15:18
Show Gist options
  • Save cowboy/60b0820e0cb957a0676b to your computer and use it in GitHub Desktop.
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
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));
@cowboy
Copy link
Author

cowboy commented Aug 12, 2014

(I didn't write this code)

@AceMood
Copy link

AceMood commented Aug 12, 2014

Want to prevent from being overwritten by third-party javascript?

@RSeidelsohn
Copy link

This is their little dirty secret.

@rbecheras
Copy link

Could you explain the purpose of this code ? What is really about ?

@calebds
Copy link

calebds commented Aug 19, 2014

@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

@skyrpex
Copy link

skyrpex commented Jun 25, 2015

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