Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    Regular Coder
    Join Date
    Sep 2002
    Location
    Surrey, UK
    Posts
    119
    Thanks
    0
    Thanked 0 Times in 0 Posts

    mozilla + document.write +xhtml

    I participate on another forum and a user there has a problem. I'll reprint the problem exactly.
    <problem>
    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).
    </problem>

    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!

  • #2
    jkd
    jkd is offline
    Senior Coder jkd's Avatar
    Join Date
    May 2002
    Location
    metro DC
    Posts
    3,163
    Thanks
    1
    Thanked 18 Times in 18 Posts
    See this bug

    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.

  • #3
    Regular Coder
    Join Date
    Sep 2002
    Location
    Surrey, UK
    Posts
    119
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #4
    jkd
    jkd is offline
    Senior Coder jkd's Avatar
    Join Date
    May 2002
    Location
    metro DC
    Posts
    3,163
    Thanks
    1
    Thanked 18 Times in 18 Posts
    Dude, nothing wrong about linking to other forums... I sometimes link to JavascriptCity, and often link to CodingForums on other sites.....

  • #5
    Senior Coder
    Join Date
    Jun 2002
    Location
    near Oswestry
    Posts
    4,508
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I thought application/xml+xhtml was unuseable anyway, due to IE trying to download rather than display pages served with this mime type..?

  • #6
    Regular Coder
    Join Date
    Sep 2002
    Location
    Surrey, UK
    Posts
    119
    Thanks
    0
    Thanked 0 Times in 0 Posts
    >> 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.

  • #7
    Regular Coder
    Join Date
    Sep 2002
    Location
    Surrey, UK
    Posts
    119
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Possible solution

    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">
    <head>
    <title></title>
    <script>
    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>

  • #8
    Regular Coder
    Join Date
    Sep 2002
    Location
    Surrey, UK
    Posts
    119
    Thanks
    0
    Thanked 0 Times in 0 Posts
    The code I posted was accepted as the solution. Interesting stuff!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •