views:

966

answers:

5

I would like to create a page that runs a 3rd party script that includes document.write after the DOM was already fully loaded.

My page is not XHTML. My problem is that the document.write is overwriting my own page. (which is what it does once the DOM was loaded).

I tried overriding the document.write function (in a way similiar to http://ejohn.org/blog/xhtml-documentwrite-and-adsense/) but that doesn't cover cases where the document.write contains partial tags.

An example that would break the above code is:

document.write("<"+"div");
document.write(">"+"Done here<"+"/");
document.write("div>");

Is there some way to modify the document.write insertion point through JavaScript? Does anyone have a better idea how to do this?

+2  A: 

Original Answer before the edit:


Basically the problem of document.write is that it does not work in XHTML documents. The most broad solution then (as harsh as it may seem) is to not use XHTML/xml for your page. Due to IE+XHTML and the mimetype problem, Google Adsense breaking (may be a good thing :), and the general shift towards HTML5 I don't think it's as bad as it seems.

However if you'd really like to use XHTML for your page, then John's script that you linked to is the best you've got at this point. Just sniff for IE on the server. If the request is from IE, don't do anything (and don't serve the application/xhtml+xml mimetype!). Otherwise drop it into the <head> of your page and you're off to the races.

Re-reading your question, is there a specific problem you have with John's script? It is known to fail in Safari 2.0 but that's about it.

Crescent Fresh
My page is not using XHTML, but I load the 3rd party scripts after the DOM was loaded, and they override my page.My problem is that the following code will fail:document.write( '<' +'div'); <br/>document.write( '>' + 'Text Content' + '<'); <br/>document.write('\div>');
yeeeev
@yeeeev: wow, so this has nothing to do with XHTML? You should really put that code into your question as a code snippet so we can help you better. It appears incorrectly as a comment.
Crescent Fresh
@yeeev: I see you actually tried putting it in already. Let me edit your question....
Crescent Fresh
A: 

In order to alter the content of the page after the DOM has rendered you need to either use a javascript library to append HTML or text at certain points (jQuery, mootools, prototype, ...) or just use the innerHTML property of each DOM element to alter/append text to it. This works crossbrowser and doesn't require any libraries.

ChrisR
A: 

There are better ways to do this. 2 ways

1) Append

<html><head>
<script>
  window.onload = function () {
    var el = document.createTextNode('hello world');
    document.body.appendChild(el);
  }
</script></head><body></body></html>

2) InnerHTML

<html><head><script>
  window.onload = function () {
    document.body.innerHTML = 'hello world';
  }
</script></head><body></body></html>
Robert Cabri
No love for the correct answer?
Allen
This solution is useless if you can't alter the 3rd party script. Also solution #2 would replace the whole body with 'hello world'. Typo?
gregers
+1  A: 

You may be interested in the Javascript library I developed which allows to load 3rd party scripts using document.write after window.onload. Internally, the library overrides document.write, appending DOM elements dynamically, running any included scripts which may use document.write as well.

Unlike John Resig's solution (which was part of the inspiration for my own code), the module I developed supports partial writes such as the example you give with the div:

document.write("<"+"div");
document.write(">"+"Done here<"+"/");
document.write("div>");

My library will wait for the end of the script before parsing and rendering the markup. In the above example, it would run once with the full string "<div>Done here</div>" instead of 3 times with partial markup.

I have set up a demo, in which I load 3 Google Ads, an Amazon widget as well as Google Analytics dynamically.

Eric Bréchemier
+2  A: 

If you're dealing with 3rd party scripts, simply replacing document.write to capture the output and stick it in the right place isn't good enough, since they could change the script and then your site would break.

writeCapture.js does what you need (full disclosure: I'm the author). It basically rewrites the script tags so that each one captures it's own document.write output and puts it in the correct place. The usage (using jQuery) would be something like:

$(document.body).writeCapture().append('<script type="text/javascript" src="http://3rdparty.com/foo.js"&gt;&lt;/script&gt;');

Here I'm assuming that you want to append to the end of the body. All jQuery selectors and manipulation methods will work with the plugin, so you can inject it anywhere and however you want. It can also be used without jQuery, if that is a problem.

noah