document.write + application/xhtml+xml + mozilla

dorward
dorward used Ask the Experts™
on
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).
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
ahosangFinance Systems Developer

Commented:
Could this be a bug? This seems to suggest it:
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.

Author

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?
Finance Systems Developer
Commented:
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" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script>
if (document.getElementById) {
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");
document.getElementsByTagName("head")[0].appendChild(l);
}
</script>

</head>

<body>
Test Page


</body>
</html>
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

ahosangFinance Systems Developer

Commented:
Any feedback? I could mess around further with a few things if you describe what's happening.
ahosangFinance Systems Developer

Commented:
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

Author

Commented:
That seems to work fine. Thank.
ahosangFinance Systems Developer

Commented:
Glad to help. This made me think more about xhtml, xml etc.

Commented:
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.  

http://www.xml.com/pub/a/2003/03/19/dive-into-xml.html


Just thought you might be interested to know!


bruno

Commented:
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

Commented:
hi korxt2,

this question is already closed and you will get no answers here, you need to ask your own question:  http://www.experts-exchange.com/Web/Web_Languages/JavaScript/askQuestion.jsp

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial