View Full Version : mozilla + document.write +xhtml

01-27-2003, 03:03 PM
I participate on another forum and a user there has a problem. I'll reprint the problem exactly.
I have a number of XHTML pages that I need to include some content in using JavaScript. (It has to be with JavaScript as it adds a style sheet that hides a menu, if users without JavaScript come to the page it is essential that the styles do not get applied).

My code:

if (document.getElementById) {
document.write("<link rel=\"stylesheet\" type=\"text/css\" href=\"/css/js.css\" media=\"screen\" />")

This works fine in a document servered as text/html, but in order to take advantage of Mozilla's XML parser I serve XHTML pages as application/xhtml+xml (which they should be served as) if the browser claims it supports that content type.

Now - how do I get this to work with the browser rendering XHTML instead of tag soup?

I do not want a flash of unstyled content (and some of the pages have a lot of content in them so there will be such a flash if the script is called using onload).

I don't mind anyone going there and helping the guy and getting the kudos themselves, but as you know it's against forum rules to advertise another forum. If anybody(Alex, jkd or anybody for that matter) wants to e-mail me for address, you're welcome. I can't suggest that he comes here because that's against site rules over there obviously.
He's a good chap and I'd like to help him. It makes me learn as well. Obviously I'll liase with him regarding comments made here, as it's not my dilemma and I don't know the full ins and outs of it.
If there is any problem with this, please moderators, e-mail me!

01-27-2003, 04:58 PM
See this bug (http://bugzilla.mozilla.org/show_bug.cgi?id=111514)

Essentially related - it deals with having the document object in a page served as application/xhtml+xml inherit from HTMLDocument, instead of XMLDocument. Inheriting from HTMLDocument also includes inheriting the document.write() method.

01-27-2003, 05:38 PM
Thanks jason that's interesting link. I'll pass this on and give credit. Maybe you have a website I can give him and say you're the author if you wish.

01-27-2003, 06:07 PM
Dude, nothing wrong about linking to other forums... I sometimes link to JavascriptCity, and often link to CodingForums on other sites..... :)

01-27-2003, 06:20 PM
I thought application/xml+xhtml was unuseable anyway, due to IE trying to download rather than display pages served with this mime type..?

01-27-2003, 06:43 PM
>> Dude, nothing wrong about linking to other forums... I sometimes link to JavascriptCity, and often link to CodingForums on other sites.....
Ok, but I certainly can't provide any link to other forums there. They're quite strict on that!!
This is quite interesting bug link that jason gave. The question of how the mime-type should be handled made for some good discussion.

01-28-2003, 01:28 AM
I offered this bit of code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
var l=document.createElementNS("http://www.w3.org/1999/xhtml","link");
l.setAttribute("rel", "stylesheet");
l.setAttribute("type", "text/css");
l.setAttribute("href", "/css/js.css");
l.setAttribute("media", "screen");


Test Page

01-29-2003, 09:42 PM
The code I posted was accepted as the solution. Interesting stuff!