Skip to content

Instantly share code, notes, and snippets.

@l
Forked from anonymous/gist:1342586
Created November 6, 2011 07:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save l/1342587 to your computer and use it in GitHub Desktop.
Save l/1342587 to your computer and use it in GitHub Desktop.
/*@cc_on @*/
/* if IE, do nothing (error 8004004, whatever that is) */
/*@if (@_jscript_version > 0)
@else @*/
document.getCurrentScript = function(){
// fails when document.write nests like
// document.write('<'+'script'+'>' +'document.write(...)' + '</'+'script'+'>')
return (function (e) {
if (!e) return null // gracefully report failure
if (e.nodeName.toLowerCase() == 'script') return e;
return arguments.callee(e.lastChild)
})(document)
};
document.__write__ = document.write;
document.write = function (what, where) {
if (!where) where = this.getCurrentScript();
if (!where) return this.__write__(what);
var here = where.previousSibling;
if (!here || here.nodeType != 1 || here.className != '.written'){
here = document.createElement('div');
here.className = '.written';
where.parentNode.insertBefore(here, where);
}
if (what.nodeType){
here.appendChild(what);
}else{
if (what.match(/^<script/i)){
// needed to have your browser reevaluate the script
this.__write__(what);
}
else{
var tmp = here.innerHTML;
try{
here.innerHTML += what;
}catch(e){
here.innerHTML = tmp;
if (here.namespaceURI == 'http://www.w3.org/1999/xhtml'){
do{
what = what.replace('&nbsp;', ' ');
}while(what.indexOf('&nbsp;')>=0)
here.innerHTML += what;
}
else {
alert(e);
}
}
}
}
};
/*@end @*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment