Feed Sign in with OpenID OpenID

Simon Willison’s Weblog

Insert in place without document.write. Very neat trick, but I’d like to see more extensive reports on browser compatibility before committing to it.

Tagged , , , ,

7 comments

  1. That's Not Write describes a more straightforward, and less obtrusive, way to address this issue.

    Sam Ruby - 12th July 2007 10:41 - #

  2. Both SitePoint and Sam's tricks are very nice and show that document.write() is not needed. They also work in every browser in popular use today, so I wouldn't worry so much about the browser compatibility. DOM2 is pretty much universally supported, at least in a way that makes this script work flawlessly across the board.

    Asbjørn Ulsberg - 12th July 2007 11:06 - #

  3. IMO, the biggest disadvantage of document.write isn't that it currently doesn't work in XHTML, but that it relies on the script being loaded in-place in a blocking manner. By "blocking" I mean that while the script loads, the browser can't continue laying out the page or even parsing to find out what other resources (images, scripts, etc) need to be fetched. By "in-place" I mean that the script is included at the point in the document where the script adds content, rather than at the end of the document or in the head using the DEFER attribute.

    Sam's trick also relies on in-place, blocking script loads, so I don't think it's the best solution.

    It's too bad that Google AdSense insists on using document.write. I'm not comfortable using AdSense anywhere other than the bottom of my page as a result. Why can't I just reference the iframe directly from my page?

    Jesse Ruderman - 12th July 2007 11:28 - #

  4. Please add https to your whitelist for the "href" attribute in comments. I had to change the bug link from https to http to make your blog stop rejecting it.

    Jesse Ruderman - 12th July 2007 11:29 - #

  5. It's in the Gecko DOM reference and on MSDN as being supported by IE5+, which leaves Safari and uncaught exceptions in IE.

    (I'd hope.)

    ben - 12th July 2007 12:54 - #

  6. Somewhat related: Mike Davidson suggested a method last month for adding non-blocking widget code to a page. That is, the rest of the page loads without waiting for the inline third-party script to load. The method that he uses creates a div (either via the very DOM method that sitepoint suggests, or via document.write) and then drops the third-party script tag into that div. It works nicely in all the major browsers.

    Josh Clark - 12th July 2007 14:07 - #

  7. I tested that technique in IE5 (windows and mac), IE5.5, 6 & 7, Safari 1.3 and 3b, Opera 9, and Firefox 2. It worked in all, however generating an iframe with it didn't work in IE5.0

    brothercake - 13th July 2007 01:28 - #

Sign in with OpenID

Auto-HTML: Line breaks are preserved; URLs will be converted in to links.

Manual XHTML: Enter your own, valid XHTML. Allowed tags are a, p, blockquote, ul, ol, li, dl, dt, dd, em, strong, dfn, code, q, samp, kbd, var, cite, abbr, acronym, sub, sup, br, pre

A django site