NEWER! Checkout PostScribe! It's awesome.
NEW! Checkout this branch for a completely rewritten writeCapture that should be much more compatible with a greater variety of scripts.
$(something).writeCapture().html('some html with document.write scripts');
or if you just want to force your ads to load async:
writeCapture can be used with or without jQuery and can be loaded asynchronously. For async loading, [[ControlJS]] is a good option, but any async loading library or technique should work. See the wiki for more detailed usage.
Need help? See "Help!" below.
writeCapture.js was developed using jQuery 1.3.2, but should work with most versions. We recommend you use jQuery, because it's pretty great, however, if that's not possible, there is support/nolib-support.js, which provides a bare bones implementation of the support functions jQuery would otherwise provide and only adds about 800 bytes when Gzipped.
To use nolib-support, just grab the latest writeCapture-x.x.x.-nolib.min.js from the downloads section. It already includes writeCapture.js, so it's the only file you need. If you'd like the unminified source for debugging, simply include nolib-support.js before writeCapture.js.
Note that nolib does not implement
onLoad, which is required for
If you already have another Ajax library you are using like Prototype or dojo, you can implement write capture support yourself.
Why should I care about writeCapture.js?
Sometimes we are forced to use a third party script or markup that we simply
cannot change (usually an Ad server). While our websites are sleek and snappy
with a nearly pristine codebase and progressive enhancement and Ajax
everywhere, many 3rd party libraries are still using tables for layout and
the dreaded, evil
document.write so evil is that it is only "useful" for scripts inside
the body tag that are processed while the page is loading. If document.write is
called anytime after page load, it wipes out all the existing page content.
That makes it very difficult to dynamically refresh content containing
Fortunately for you, difficult is not impossible, and writeCapture.js has already written (and extensively tested) the difficult part for you. All you have to do is give it the offending evil HTML and it will return nice clean HTML, safe for injection into the document.
TheDailyBeast.com uses writeCapture.js.
8tracks.com "We were having horrible latency with our ad providers and your plugin saved the day! Very impressive work!"
If you use writeCapture and want some free advertising, let us know!
First, please read the wiki, particularly the Options page. There are a lot of workarounds to try before giving up.
Next, ask your question on the mailing list. Please ask your question the smart way. That includes creating a stripped down and working example at http://jsfiddle.net/ 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. 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.
AdSense - AdSense code using
document.writewill not work. However AdSense provides asynchronous tracking support, so you don't even need writeCapture to load AdSense asynchronously.
If any of the included scripts are on another domain, they will have to be loaded asynchronously via script tag injection. Subsequent scripts in the HTML will be blocked, so order will be preserved, but this means that not all scripts may have yet run after the HTML is injected. This means that if your script depends on all the scripts in the HTML having run, it should utilize the done() callback.
Scripts that assume that they are the last element in the document will probably not function properly. This is rare, but if a script is uncouth enough to use document.write, it's a possibility.
Control JS Support
The built in
document.write support in Control JS
is not great. It adds extra
span tags and is not nearly as robust as a full library like writeCapture. That's why writeCapture provides a plugin to replace Control JS's
document.write support with writeCapture.js.
For usage, see the wiki.
// call from $.ready() or your document ready of choice writeCapture.extsrc(doneCallback);
You can use the same markup (
asyncsrc="...") but with a few
- All the power of writeCapture.js, so more
document.writeedge cases will work.
- No extra span tags!
- All scripts with
extsrcwill be run in order, so you don't have to worry about dependencies.
Note that you do not need extsrc.js, we are simply supporting the same syntax in writeCapture.
proxyGetElementByIdwill attempt to copy attributes from the proxy when jQuery is present. Should improve compatibility in some cases.
writeOnGetElementByIdfor when proxying isn't enough.
New Experimental Feature:
autoAsync. Inspired by eligrey's async-document-write.
All options and hacks can now be set globally and/or per call.
No more eval! Fixes a problem in IE where
varvariables were not visible to other scripts.
If it's good enough for newsweek.com , it's good enough to be version 1.0.
Fixed bug in pause/resume logic when using asyncAll that was preventing execution from resuming.
Fixed error in IE when using
Big overhaul of the internal queuing system. It should be more robust and 100% deterministic from this point forward. Known to help with deeply nested cross domain scripts.
Fixed issue where scripts that include newlines in their src attribute were being truncated.
Added debugging support. See support/debug-support.js.
writeCapture.js has matured enough to be close to a 1.0 release, after a little seasoning. If you have a problem with your Ad server or other code, submit a bug report!
Added hacks to support scripts that combine
document.writewith DOM manipulation and iframes. Thanks to jcugno for the bug reports and example code. Enabling
proxyGetElementByIdshould help with some Ad servers.
- Fixed a number of issues with 'marginal' scripts (but if all scripts were in good shape, you wouldn't need writeCapture). Should help with some ad servers. Hat tip to neskire for raising the issues.
- Bugfix - jQuery 1.4.1 has a bug in replaceWith that mishandles strings. We work around it by not using replaceWith.
Added fixUrls hack to deal with encoded URLs.
Added MIT and GPL licenses, applied retroactively to all versions.
Optimized script loading (start loading in parallel immediately) if asyncAll is enabled.
Created jQuery plugin.
Run all sanitized scripts through a global queue to prevent problems when multiple sanitizes with async loads are run in sequence.
Only tasks that were running async (read: async script loading with pause/resume) will result in a call to defer.
Allowed replacement of support functions by defining writeCaptureSupport so writeCapture can be used without jQuery.
Added nolib-support.js for using writeCapture without jQuery.
- first release. Any content but a single script tag would result in partially async execution thanks to an overuse of defer.