Forum Moderators: open

Message Too Old, No Replies

Is it possible to read the!DOCTYPE with j/s?

         

Lance

6:54 pm on Oct 31, 2004 (gmt 0)

10+ Year Member



Is there any way with j/s to return the <!DOCTYPE ...> declaration of a page?

Rambo Tribble

7:52 pm on Oct 31, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The DOM 1 specification includes the doctype property as part of the document object. It can be referenced with "document.doctype". Various values of the property can be queried, "name" being the best supported. Unfortunately, IE for Windows has never implemented this aspect of the standard DOM.

Try this in anything but IE Win:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Untitled</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
</head>
<body>
<script type="text/javascript">
var dt=document.doctype;
alert(dt.name+"\n"+dt.publicId+"\n"+dt.systemId);
</script>
</body>
</html>

Lance

8:01 pm on Oct 31, 2004 (gmt 0)

10+ Year Member



Thanks Rambo Tribble. Unfortunately, it is specifically in IE that I need this. *sigh*

Okay then, is there any way to just read the source of the currently loaded document without having to requery the server? I've tried reading document.documentElement.outerHTML and that gets everything but the!DOCTYPE.

Span

8:08 pm on Oct 31, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Copied from a page that isn't on the web anymore. Never used it and I don't know in which browsers it works. But maybe it helps a bit.

<script type="text/javascript">
strict=false
var d=document.doctype
strict=(document.compatMode=="CSS1Compat")
strict=(d&&d.systemId?(d.systemId.indexOf("strict")>-1?true:(d.publicId.indexOf("transitional")>-1?true:false)):(d&&d.publicId.indexOf("transitional")==-1?true:strict))
strict=(d&&d.name.indexOf(".dtd")>-1)?true:strict
alert("strict= "+strict)
</script>

Sorry for the sidescroll..