Jump to content

XHTML mistake.


turtle

Recommended Posts

hi,I saw nothing about different content types in the XHTML section.(In fact, w3schools.org pages are served as text/html, not as xml.)I recommand those readings about the importance of content-types in XHTML :* http://www.webdevout.net/articles/beware_of_xhtml.php* http://hixie.ch/advocacy/xhtmlIn short, when you send XHTML as text/html (and IE shows a download prompt if you send the correct content-type), the browser parses it as an HTML document, and it understands new things like the self-closing tags as old HTML errors. So it's even harder to parse than an old well-formed HTML document.Served as text/html, XHTML is as useful as HTML4.01 strict, maybe a little more strict, even if you can write strict HTML4.01 documents too.XHTML is really needed when you need to mix multiple XML documents, as XHTML+MathML+SVG (see http://www.w3.org/TR/XHTMLplusMathMLplusSVG/ ).I hope it'll help tou to correct your XHTML lessons.Good luck.

Link to comment
Share on other sites

Well, you're right, and you're not right.You're right that XHTML should be served with the MIME type application/xhtml+xml but as you said yourself, when you do that, IE shows a promt box for download instead of displaying the page. W3Schools should mention this MIME type problem, but I don't think they should suggest using the correct MIME type scince IE's problems.Also, you are not exactly right that XHTML served as text/html is as useful as HTML 4. When files are composed with XML syntax, then become acessable to other XML based languages. For example, I can "grab" XHTML content from W3Schools with XSLT located on another domain and display that content on that other site. I've actually done that, but only for testing purposes... I respect W3Schools too much to "steal" without permission (just think how silly this sounds). Also, this XML syntax allows server side and client side scripting languages to treat the code as XML instead of HTML, allowing them to perfom XML specific actions, etc.There's also the thing that correct MIME types allows faster rendering scince less rules are loaded and considered, but still- until there's an effective solution to serve IE with text/html and the rest with application/xhtml+xml, the topic remains open.

Link to comment
Share on other sites

...There's also the thing that correct MIME types allows faster rendering scince less rules are loaded and considered, but still- until there's an effective solution to serve IE with text/html and the rest with application/xhtml+xml, the topic remains open.

Use a simple server-side script to determine browser type. Here's what a simple server-side browser sniffer looks like in Java ServerPages (JSP):
<%@ page import="java.util.StringTokenizer,java.util.Vector" %><%      String browser = request.getHeader("User-Agent");      StringTokenizer tokenizer = new StringTokenizer(browser, " ");      Vector userAgent = new Vector();      boolean containsOpera = false;      boolean containsKonqueror = false;      boolean containsFirefox = false;      boolean containsIE = false;      while (tokenizer.hasMoreTokens()) {            userAgent.add(tokenizer.nextToken());      }      for (int i = 0; i < (userAgent.size()); i++) {            if ((("" + userAgent.get(i)).length() >= 5) && ((("" + userAgent.get(i)).substring(0, 5)).equalsIgnoreCase("opera"))) {                  containsOpera = true;            }            if ((("" + userAgent.get(i)).length() >= 9) && ((("" + userAgent.get(i)).substring(0, 9)).equalsIgnoreCase("konqueror"))) {                  containsKonqueror = true;            }            else if (("" + userAgent.get(i)).equalsIgnoreCase("msie")) {                  containsIE = true;            }            else if ((("" + userAgent.get(i)).length() >= 7) && ((("" + userAgent.get(i)).substring(0, 7)).equalsIgnoreCase("firefox"))) {                  containsFirefox = true;            }      }      if (containsOpera && !(containsFirefox) && !(containsKonqueror)) {%><%@ include file="opera.txt" %><%      }      else if (containsKonqueror) {%><%@ include file="konqueror.txt" %><%      }      else if (containsFirefox) {%><%@ include file="firefox.txt" %><%      }      else if (containsIE) {%><%@ include file="ie.txt" %><%      }      else if (!(containsOpera) && !(containsKonqueror) && !(containsFirefox) && !(containsIE)) {%><%@ include file="other.txt" %><%      }%>

As you can see, it's fairly simple (at least in Java) to determine what browser someone is using, and then return the proper content. If you want to see it in action, the script is running here. The page is for customizations to the Opera browser (still a bit of a work in progress). I made it to give an offensive message if you are not using Opera when you view the page. It will only load the main content if it thinks you are using Opera.Say that you want to set a special content type based on whether the user is viewing the page in IE, or another browser. That would go something like this:

<%@ page import="java.util.StringTokenizer,java.util.Vector" %><%      String browser = request.getHeader("User-Agent");      boolean containsOpera = false;      boolean containsIE = false;      StringTokenizer tokenizer = new StringTokenizer(browser, " ");      Vector userAgent = new Vector();      while (tokenizer.hasMoreTokens()) {            userAgent.add(tokenizer.nextToken());      }      for (int i = 0; i < (userAgent.size()); i++) {            if ((("" + userAgent.get(i)).length() >= 5) && ((("" + userAgent.get(i)).substring(0, 5)).equalsIgnoreCase("opera"))) {                  containsOpera = true;            }            else if (("" + userAgent.get(i)).equalsIgnoreCase("msie")) {                  containsIE = true;            }      }      if (containsOpera) {            response.setContentType("application/xhtml+xml");            //Since Opera can send a user-agent string almost identical to MSIE6, it's best to sniff for it first.      }      else if (containsIE) {            response.setContentType("text/html");      }      else if (!(containsOpera) && !(containsIE)) {            response.setContentType("application/xhtml+xml");      }%>

To see that script in action, just navigate here. I'm sure it's just as simple to do that in PHP as well. :)

Edited by Jonas
Link to comment
Share on other sites

  • 2 weeks later...

Here's an even easier way of serving the proper doctype, ans it's even in PHP:

<?    if(stristr($_SERVER["HTTP_ACCEPT"],"application/xhtml+xml")){        header("Content-Type: application/xhtml+xml; charset=UTF-8");        echo('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">');    } else {        header("Content-Type: text/html; charset=UTF-8");        echo ('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">');    }?>

This simple PHP scriptlet also outputs the proper doctype as well. XHTML 1.1 if the browser supports application/xhtml+xml or XHTML 1.0 Strict if the browser does not. You can of course change the doctype declarations to whatever you want.BTW: This script complements of HTMLDog.com...

Link to comment
Share on other sites

  • 1 month later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...