I think the real answer here is "don't use document.write" :-) See this related SO post for why:
Why is document.write considered a "bad practice"?
So what should you do if not document.write? Well, ideally JQuery (it's all but a requirement for writing JS nowadays IMHO). With jQUery your problem is as simple as:
$(document.body).append(
"<object><embed type = 'application/x-java-applet;version=1.5' \ CODE = ...");
If you don't want to use jQuery (not-so-subliminal message: use jQuery!) you can also use either innerHTML (as Kiva suggested), or document.createElement + document.body.appendChild to add the element to the page.
I suspect if you use any of these techniques, instead of document.write, you'll see similar behavior to just having the element there in the HTML in the first place.