Skip to content
Utility to assist the Ajax loading of HTML containing script tags that use document.write. Mailing List:
JavaScript Groovy
Find file
Pull request Compare This branch is 17 commits ahead, 11 commits behind master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.

Usage Summary


writeCapture2 offers a simple API:

writeCapture.write(document.getElementById('bar'),'<script src="anotherScript.js">...');
writeCapture.write(function() {
    alert('all document.writes are done!');

writeCapture.write will ensure everything is run in order. Passing a function will call that functions when all the previous writes have finished and before any writes that follow it.


The advanced API has the same semantics as document.write, but targets an element instead of a document or iframe:

writeCapture(element,doneCallback).write('some html with document.write scripts').close();

Remember to call close.

Note that it accepts a single, unwrapped DOM element. If you have a jQuery object, you'll need to unwrap it before passing it to writeCapture.

The advanced API will happily let you run multiple scripts at a time which can cause problems. Unless you need to write partial HTML fragments, you should always use the simple API.


writeCapture2 depends only on the element.write library.


Always ask your question on the mailing list. Please ask your question the smart way. That includes creating a stripped down and working example at or so that everyone can play with it. A private page on your site demonstrating the problem is rarely helpful because we can't go and edit the source. Please take the time to reproduce it on JS fiddle so we can help you.

Please do not email the author privately or open an issue in the issue tracker unless you have a test case showing you've found a real bug. The author does not actively use write capture, so he probably does not have any idea what is causing your problem.

You may think that it is quicker to go straight to the mailing list or the author without taking the time to isolate the problem and fully read and comprehend the documentation and go over the source code, but anyone who can help you has already done all of those things. Please respect our time, and you will get enthusiastic help.

Something went wrong with that request. Please try again.