So I ended up doing some last-minute fiddling with the Really Simple History 0.6 beta before posting it for testing over at Google Code. Matt Snider pointed me to a potential SSL problem with using DOM methods rather than document.write to insert IE’s hidden iframe. I’ve got four places where I need to insert stuff into the DOM, and I had already hemmed and hawed quite a bit about whether I should use DOM methods, document.write or a mixture. For now, based on Matt’s comments and the verbosity of DOM methods, I’ve reverted back to using dirty-but-concise document.write. I’m going to continue testing this in as many browsers as possible before settling on a permanent solution.
While fiddling, I discovered something interesting that contradicted yesterday’s (now-corrected) post. I originally wrote that if you’ve got script-generated DOM elements that need to be treated as native to the document (so, for instance, you can persist form values across an entire session regardless of whether you navigate off-site), you must use document.write to insert them into the DOM. Not true, as it turns out. It doesn’t matter whether you call document.write or standard DOM methods from the head of your document. Either way, both methods seem to result in "native" HTML form fields that can properly persist values. Looking at Firebug or Opera’s developer console, you can see that these elements end up being children of the body element even though they were, in fact, inserted into the head element. Modern browsers are so smart!
This whole issue of when to generate DOM nodes brings up an interesting facet of library development: Deciding how much stuff the end user (i.e the developer who is leveraging your library) has to do in the actual document. For most libraries, it’s not an issue, because their objects and methods aren’t going to do anything until called by application code. But it’s different for something like Really Simple History, which relies on a variety of browser-specific hacks, many of which need to be set up before the DOM finishes loading. Sure, you could include inline script blocks in the body of your document to write the hidden, browser-specific DOM elements that enable your library to function. But developers really hate it when the use of a library forces them to fiddle with their page templates. Library authors have to get creative – for instance, by calling their initialization methods in the library file itself, even though that will result in DOM elements getting written to the document head.
As I said in yesterday’s post, Ajax history management works only thanks to a variety of browser quirks and hacks. If you want clean code that doesn’t break any standards, then you should cross history management off your list of requirements.
Things get even more interesting when your browser-specific DOM hacks work best when they’re included as actual markup. My beta is out there, but I’m still scrambling to figure out the best strategy for Opera which, thanks to a bug introduced in 9.x, relies on a hack involving an image tag a crazy URL:
<img src="javascript:location.href=’javascript:dhtmlHistory.checkLocation();’;" style="visibility:hidden" />
This image throws errors in IE, so I couldn’t tell developers to include it in their actual markup even if I wanted to release a library with such brittle implementation rules. If I want it to go in the actual HTML, I’d have to ask developers to do server-side browser sniffing and include it only for Opera users. YUCK. Yet all of the methods of writing it to the DOM via script have drawbacks. The way I’m doing it now, it works in Opera, but it throws an exception in the JavaScript console. Sure, only developers will see that exception, but still…. I guess I still have my work cut out for me.
