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
dorwardAsked:
Who is Participating?
 
ahosangConnect With a Mentor Finance 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" "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>
0
 
ahosangFinance Systems DeveloperCommented:
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.
0
 
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?
0
Easily Design & Build Your Next Website

Squarespace’s all-in-one platform gives you everything you need to express yourself creatively online, whether it is with a domain, website, or online store. Get started with your free trial today, and when ready, take 10% off your first purchase with offer code 'EXPERTS'.

 
ahosangFinance Systems DeveloperCommented:
Any feedback? I could mess around further with a few things if you describe what's happening.
0
 
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
0
 
dorwardAuthor Commented:
That seems to work fine. Thank.
0
 
ahosangFinance Systems DeveloperCommented:
Glad to help. This made me think more about xhtml, xml etc.
0
 
brunoCommented:
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
0
 
k0rxt2Commented:
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
0
 
brunoCommented:
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
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.