What's wrong is that you're using document.write() — or at least the author of that script is using it. That's what document.write() does when it's called after the actual original document has been built by the browser. I suppose your code is in some jQuery initialization block:
$(function() {
  // your code somewhere in here ...
});
Thus it runs only when the browser has gotten the page all ready.  But at that point, a new call to document.write() will be interpreted as meaning that you want to completely replace the current page with new stuff.
One thing to try would be to put an <iframe> in the document (not with document.write()!!) and then put the <script> tag inside that.