JavaScript: deferred document.write implementation
JavaScript HTML PHP
Switch branches/tags
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.
lazywrite-min.js bump v1.2.2: fixed issue #6 - half opened tag issue. May 22, 2015

LazyWrite (deferred document.write implementation)

LazyWrite is build to have the best compromise between a good page load and write a complex external data in your page (like Advertising). This allow you to control when should those data start to render or load.
Size: less than 3.4KB (2KB gzipped) using UglifyJS.


  • Increase highly the page load
  • Stop freezing client navigator
  • Forget iframe calls
  • No other library required (like jQuery)
  • No(or less) change of existing code

Browser Support:

  • IE [tested on v6+]
  • FireFox [tested on v2+]
  • Chrome [tested on v5+]
  • Safari [tested on v4+]
  • Opera [tested on v9.6+]

API: ("LazyWrite" global variable)


  • original document.write function.


  • Replaces original document.write() with lazy write.
  • This function will auto executes when "lazywrite.js" loaded.
  • To prevent auto executing, sets AUTO_LAZYWRITE = false; before loads "lazywrite.js".

.render(content String [, holderId String] [, callback(errors Array) Function])

  • postpones content rendering in the given holder element or current place (like document.write()).
  • An array of all caught exceptions will passes to the callback().


  • Starts to render all contents.
  • When all content be rendered, it will restores the original document.write().

.findScripts([String type])

  • Finds and stacks all custom typed script elements.

As AMD Module

  • No global variable LazyWrite.
  • no .prepare() auto executing.


  • Can't use document.getElement*() functions to get the writing element in the processing.

Example: Google AdSense

<script src="lazywrite-min.js"></script>
    google_ad_client = "ca-pub-5840687392233497";
    google_ad_slot = "9902234827";
    google_ad_width = 728;
    google_ad_height = 90;
    document.write('<script src="//"><\/script>');
    setTimeout(LazyWrite.process, 3000); // delay 3sec