document.write + application/xhtml+xml + mozilla

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).
LVL 17
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ahosangFinance Systems DeveloperCommented:
Could this be a bug? This seems to suggest it:

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.
dorwardAuthor Commented:
It could be a bug, but it doesn't stop me needing to deal with it. So it seems I'm looking for a work around. Any takers?
ahosangFinance Systems DeveloperCommented:
darn, reading throught the bug thread more carefully, I was hoping to find a easy workaround. It seems to be either a bug, or uncertain implementation issue, but as you say, you have to DEAL with it.
So, you cannot serve as text/html because you need XML parsing, but you do not have document.write() available in XML document because it doesn't have that HTMLDocument method. The workaround may be to use XML methods - createElementNS specifically. I take it you already have xhtml namespace declared in <html>(as below) etc.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="">
if (document.getElementById) {
var l=document.createElementNS("","link");
l.setAttribute("rel", "stylesheet");
l.setAttribute("type", "text/css");
l.setAttribute("href", "/css/js.css");
l.setAttribute("media", "screen");


Test Page


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Maximize Customer Retention with Superior Service

The IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more to help build customer satisfaction and retention.

ahosangFinance Systems DeveloperCommented:
Any feedback? I could mess around further with a few things if you describe what's happening.
ahosangFinance Systems DeveloperCommented:
How's it going? Any good?
I'm not harassing because of any interest in points or whatever. It's just that I find questions like these very interesting(using DOM methods, Mozilla etc.), unlike the usual boring questions here! So, I'm really interested in progress as this was a learning experience for me too.
I can't replicate your exact situation, but I saved as .xhtml file(even works as .xml) and it seemed to work in Mozilla 1.1
dorwardAuthor Commented:
That seems to work fine. Thank.
ahosangFinance Systems DeveloperCommented:
Glad to help. This made me think more about xhtml, xml etc.
hey David et all,

Thought you might be interested, I was reading some of the latest feeds in my RSS reader and came across this article, which linked to this particular question.

Just thought you might be interested to know!

I am trying to pass data from a javascript form and display the input in another webpage, such as thank you for you informantion and list that info on the thankYou.html page
hi korxt2,

this question is already closed and you will get no answers here, you need to ask your own question:
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Development Software

From novice to tech pro — start learning today.