DOCTYPE detector

J

Jim Michaels

I can't get any "universal" code working that tries to detect whether the
document it's in is xhtml or html.
I found this, which tells me I have a hill to climb with no equipment.
http://javascript.about.com/library/bliebug.htm

I was going to use the document.doctype property if I could, but apparently
that isn't available unless I use strict. (just tried it with Strict, still
doesn't do anything).

here's what I've got. anybody got ideas that work or some pointers? I
don't have any money for books right now, and I wouldn't know which of 100's
of JS books to pick from.

I am using IE6, but I want this to be cross-browser.

<!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>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>
<script language="JavaScript" type="text/javascript">
//document.getElementsByTagName('!DOCTYPE') generates an "object", but what
type? element? if so, why won't element properties work?
document.write(document.doctype); //prints nothing
document.write(document.getElementsByTagName('html').getAttribute("xmlns"));
//prints nothing
document.write(document.getElementsByTagName('!DOCTYPE').hasAttribute("-//W3C//DTD
XHTML 1.0 Transitional//EN")); //prints nothing
//if (document.getElementsByName('!DOCTYPE') != null ||
document.getElementsByName('html').getAttribute('xmlns') != null) {
// document.write(document.getElementsByName('html').innerHTML);
//}
</script>

</body>
</html>

I'm really frustrated. I lack info.

Jim Michaels
 
D

darwinist

Jim said:
I can't get any "universal" code working that tries to detect whether the
document it's in is xhtml or html.
I found this, which tells me I have a hill to climb with no equipment.
http://javascript.about.com/library/bliebug.htm

I was going to use the document.doctype property if I could, but apparently
that isn't available unless I use strict. (just tried it with Strict, still
doesn't do anything).

here's what I've got. anybody got ideas that work or some pointers? I
don't have any money for books right now, and I wouldn't know which of 100's
of JS books to pick from.

I am using IE6, but I want this to be cross-browser.

<!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>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>
<script language="JavaScript" type="text/javascript">
//document.getElementsByTagName('!DOCTYPE') generates an "object", but what
type? element? if so, why won't element properties work?
document.write(document.doctype); //prints nothing
document.write(document.getElementsByTagName('html').getAttribute("xmlns"));
//prints nothing
document.write(document.getElementsByTagName('!DOCTYPE').hasAttribute("-//W3C//DTD
XHTML 1.0 Transitional//EN")); //prints nothing
//if (document.getElementsByName('!DOCTYPE') != null ||
document.getElementsByName('html').getAttribute('xmlns') != null) {
// document.write(document.getElementsByName('html').innerHTML);
//}
</script>

</body>
</html>

I'm really frustrated. I lack info.

Why do you need to know it, first of all?
 
J

Jim Michaels

Why do you need to know it, first of all?

because generated singleton elements are different, such as <br> or <br />
depending on whether it's html or xhtml.
 
D

darwinist

Jim said:
because generated singleton elements are different, such as <br> or <br />
depending on whether it's html or xhtml.

What are you trying to do with them? Can you use the same javascript
calls to make, get or manipulate them, regardless of the format of the
text? I'm not very familiar with xhtml I'm just thinking if it's so
hard it might not be necessary.
 
R

Randy Webb

Jim Michaels said the following on 8/5/2006 1:41 AM:
because generated singleton elements are different, such as <br> or <br />
depending on whether it's html or xhtml.

With IE, it's irrelevant as IE doesn't support - in any form - XHTML so
feeding it <br /> leads to tag soup parsing.
 
J

Jim Michaels

I forgot to mention that all the calls I made do not work (and should
theoretically work)
really what I am building is an xhtml document detector (true/false).
What are you trying to do with them? Can you use the same javascript
calls to make, get or manipulate them, regardless of the format of the
text? I'm not very familiar with xhtml I'm just thinking if it's so
hard it might not be necessary.
real simple. things like:
if (isXhtml) {document.write('<br />');} else {document.write('<br>');}

xhtml affects things like singleton elements/tags like <br> which become <br
/> and <input ...> which becomes <input ... />
it also forces element and attribute names to be lower case.

....I tried what you suggested, but got nowhere. that's why I am looking for
outside help. Apparently MSIE exposes other properties and methods for
Document object, and other related objects like Element, my two toughies.
methods/properties that are not exposed if you specify a Strict !DOCTYPE and
are used by a number of slightly older browsers.
 
G

gonaumov

Hi mr Michaels.
Only for fun i wrote this code. I tried it under Mozilla Firefox,
Opera 9.0 and
Internet Explorer 6.0. If there is a doctype in the document the
function detectDoctype() will return
object with 3 properties - xhtml - XHTML ot HTML , version - numer of
version and importance - stict, transitional etc. If there is no
DOCTYPE it will return null.
Best Regards.
/******************************
Version info "object"
******************************/
function versionInfo()
{
this.xhtml="";
this.version="";
this.importance="";
}
function detectDoctype(){
var re=/\s+(X?HTML)\s+([\d\.]+)\s*([^\/]+)*\//gi;
var myversionInfo=new versionInfo();
/*********************************************
Just check for internet explorer.
**********************************************/
if(typeof document.namespaces != "undefined"){
if(document.all[0].nodeType==8)
re.exec(document.all[0].nodeValue);
else
return null;
}else{
if(document.doctype != null)
re.exec(document.doctype.publicId);
else
return null;
}
myversionInfo.xhtml=RegExp.$1;
myversionInfo.version=RegExp.$2;
myversionInfo.importance=RegExp.$3;
return myversionInfo;
}
var myversionInfo=detectDoctype();
if(myversionInfo != null){
alert(myversionInfo.xhtml);
alert(myversionInfo.version);
alert(myversionInfo.importance);
}
else{
alert("There is no DOCTYPE in the code!");
}

Jim Michaels напиÑа:
 
E

Eric Bohlman

real simple. things like:
if (isXhtml) {document.write('<br />');} else
{document.write('<br>');}

xhtml affects things like singleton elements/tags like <br> which
become <br /> and <input ...> which becomes <input ... />
it also forces element and attribute names to be lower case.

Why not just use DOM methods to create *elements*, rather than writing
*tags* and making the browser's HTML parser do the work of turning them
into elements? That way you don't need to know/care how the tags were
written.
 
M

Martin Honnen

Jim said:
real simple. things like:
if (isXhtml) {document.write('<br />');} else {document.write('<br>');}

xhtml affects things like singleton elements/tags like <br> which become <br
/> and <input ...> which becomes <input ... />
it also forces element and attribute names to be lower case.

You are making your life more complicated than it is. If you serve your
documents as text/html then the browser uses its HTML tag soup parser
and not XML parser and you can do all document.write('<br>') as much as
you like. If you served your documents as application/xhtml+xml to have
them parsed by an XML parser then IE would not render them at all and
Mozilla or Opera would not support document.write at all.
Thus as long as you want to use document.write your are in an
environment where HTML rules rule and there is no need to discover any
DOCTYPE or parsing mode.
 
G

gonaumov

I thing in this way the code is much better and short.
function detectDoctype(){
var re=/\s+(X?HTML)\s+([\d\.]+)\s*([^\/]+)*\//gi;
var res=false;
/*********************************************
Just check for internet explorer.
**********************************************/
if(typeof document.namespaces != "undefined")
res=document.all[0].nodeType==8 ?
re.test(document.all[0].nodeValue) : false;
else
res=document.doctype != null ?
re.test(document.doctype.publicId) : false;
if(res){
res=new Object();
res['xhtml']=RegExp.$1;
res['version']=RegExp.$2;
res['importance']=RegExp.$3;
return res;
}else{
return null;
}
}
var myversionInfo=detectDoctype();
if(myversionInfo != null){
alert(myversionInfo.xhtml);
alert(myversionInfo.version);
alert(myversionInfo.importance);
}
else{
alert("There is no DOCTYPE in the code!");
}
(e-mail address removed) напиÑа:
Hi mr Michaels.
Only for fun i wrote this code. I tried it under Mozilla Firefox,
Opera 9.0 and
Internet Explorer 6.0. If there is a doctype in the document the
function detectDoctype() will return
object with 3 properties - xhtml - XHTML ot HTML , version - numer of
version and importance - stict, transitional etc. If there is no
DOCTYPE it will return null.
Best Regards.
/******************************
Version info "object"
******************************/
function versionInfo()
{
this.xhtml="";
this.version="";
this.importance="";
}
function detectDoctype(){
var re=/\s+(X?HTML)\s+([\d\.]+)\s*([^\/]+)*\//gi;
var myversionInfo=new versionInfo();
/*********************************************
Just check for internet explorer.
**********************************************/
if(typeof document.namespaces != "undefined"){
if(document.all[0].nodeType==8)
re.exec(document.all[0].nodeValue);
else
return null;
}else{
if(document.doctype != null)
re.exec(document.doctype.publicId);
else
return null;
}
myversionInfo.xhtml=RegExp.$1;
myversionInfo.version=RegExp.$2;
myversionInfo.importance=RegExp.$3;
return myversionInfo;
}
var myversionInfo=detectDoctype();
if(myversionInfo != null){
alert(myversionInfo.xhtml);
alert(myversionInfo.version);
alert(myversionInfo.importance);
}
else{
alert("There is no DOCTYPE in the code!");
}

Jim Michaels напиÑа:
I can't get any "universal" code working that tries to detect whether the
document it's in is xhtml or html.
I found this, which tells me I have a hill to climb with no equipment.
http://javascript.about.com/library/bliebug.htm

I was going to use the document.doctype property if I could, but apparently
that isn't available unless I use strict. (just tried it with Strict, still
doesn't do anything).

here's what I've got. anybody got ideas that work or some pointers? I
don't have any money for books right now, and I wouldn't know which of 100's
of JS books to pick from.

I am using IE6, but I want this to be cross-browser.

<!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>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>
<script language="JavaScript" type="text/javascript">
//document.getElementsByTagName('!DOCTYPE') generates an "object", but what
type? element? if so, why won't element properties work?
document.write(document.doctype); //prints nothing
document.write(document.getElementsByTagName('html').getAttribute("xmlns"));
//prints nothing
document.write(document.getElementsByTagName('!DOCTYPE').hasAttribute("-//W3C//DTD
XHTML 1.0 Transitional//EN")); //prints nothing
//if (document.getElementsByName('!DOCTYPE') != null ||
document.getElementsByName('html').getAttribute('xmlns') != null) {
// document.write(document.getElementsByName('html').innerHTML);
//}
</script>

</body>
</html>

I'm really frustrated. I lack info.

Jim Michaels
 
R

Randy Webb

(e-mail address removed) said the following on 8/5/2006 2:50 PM:
I thing in this way the code is much better and short.

It still doesn't do anything useful as far as anything of relevance.
It's a good exercise but that's all.

If the document is served with the proper MIME type, then the server
knows what it is and can tell the document what it is.

If it isn't served with the proper MIME type, then its tag soup and the
DTD is still irrelevant.
 
J

John G Harris

Jim Michaels said:
I can't get any "universal" code working that tries to detect whether the
document it's in is xhtml or html.

Your first problem is that most documents in the World Wide Web are
neither :-(

The simplest solution is

var i_am_xhtml = true; // Or false, as appropriate

John
 
J

Jim Michaels

I think in this way the code is much better and short.
function detectDoctype(){
var re=/\s+(X?HTML)\s+([\d\.]+)\s*([^\/]+)*\//gi;
var res=false;
/*********************************************
Just check for internet explorer.
**********************************************/
if(typeof document.namespaces != "undefined")
res=document.all[0].nodeType==8 ?
re.test(document.all[0].nodeValue) : false;
else
res=document.doctype != null ?
re.test(document.doctype.publicId) : false;
if(res){
res=new Object();
res['xhtml']=RegExp.$1;
res['version']=RegExp.$2;
res['importance']=RegExp.$3;
return res;
}else{
return null;
}
}
var myversionInfo=detectDoctype();
if(myversionInfo != null){
alert(myversionInfo.xhtml);
alert(myversionInfo.version);
alert(myversionInfo.importance);
}
else{
alert("There is no DOCTYPE in the code!");
}


terse is not necessarily better. as long as the code is reasonably
readable.
this 2nd version has brace matching errors.
 
G

Georgi Naumov

I was tested this version in Opera, Mozilla and Internet explorer and
it worked nice.
Where is the brace matching errors?
Jim Michaels напиÑа:
 
J

Jim Michaels

I was tested this version in Opera, Mozilla and Internet explorer and
it worked nice.
Where is the brace matching errors?
Jim Michaels ??????:
I think in this way the code is much better and short.
function detectDoctype(){
var re=/\s+(X?HTML)\s+([\d\.]+)\s*([^\/]+)*\//gi;
var res=false;
/*********************************************
Just check for internet explorer.
**********************************************/
if(typeof document.namespaces != "undefined")
res=document.all[0].nodeType==8 ?
re.test(document.all[0].nodeValue) : false;
else
res=document.doctype != null ?
re.test(document.doctype.publicId) : false;
if(res){
res=new Object();
res['xhtml']=RegExp.$1;
res['version']=RegExp.$2;
res['importance']=RegExp.$3;
return res;
}else{
return null;
}
}


right here causes end of function.
 
J

Jim Michaels

John G Harris said:
Your first problem is that most documents in the World Wide Web are
neither :-(

well, not if you have bought dreamweaver 8 or macromedia studio 8. default
document type is xhtml. which is what I have.
 
R

Randy Webb

Jim Michaels said the following on 8/6/2006 9:32 PM:
well, not if you have bought dreamweaver 8 or macromedia studio 8. default
document type is xhtml. which is what I have.

Too bad you paid that much money for a program that attempts to produce
documents that are not supported by about 85% of the browsers in use.

Besides, using an XHTML DTD doesn't make a document XHTML to the browser.
 
G

Georgi Naumov

That's funny I just show how the function can be used. Never mind.

Jim Michaels напиÑа:
I was tested this version in Opera, Mozilla and Internet explorer and
it worked nice.
Where is the brace matching errors?
Jim Michaels ??????:
I think in this way the code is much better and short.
function detectDoctype(){
var re=/\s+(X?HTML)\s+([\d\.]+)\s*([^\/]+)*\//gi;
var res=false;
/*********************************************
Just check for internet explorer.
**********************************************/
if(typeof document.namespaces != "undefined")
res=document.all[0].nodeType==8 ?
re.test(document.all[0].nodeValue) : false;
else
res=document.doctype != null ?
re.test(document.doctype.publicId) : false;
if(res){
res=new Object();
res['xhtml']=RegExp.$1;
res['version']=RegExp.$2;
res['importance']=RegExp.$3;
return res;
}else{
return null;
}
}


right here causes end of function.

var myversionInfo=detectDoctype();
if(myversionInfo != null){
alert(myversionInfo.xhtml);
alert(myversionInfo.version);
alert(myversionInfo.importance);
}
else{
alert("There is no DOCTYPE in the code!");
}


terse is not necessarily better. as long as the code is reasonably
readable.
this 2nd version has brace matching errors.
 
J

Jim Michaels

I was tested this version in Opera, Mozilla and Internet explorer and
it worked nice.
Where is the brace matching errors?
Jim Michaels ??????:

after a mistaken post, no brace matching error. just me misreading the
code. usually functions are separated from each other and from other code
with at least 1 blank line as a rule (usually 3), and I thought the
following test code was part of the function.

however, I tested it with a document that had no doctype, and it reported
xhtml instead of HTML. How would you get it to report html? with no
doctype, documents default to HTML.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,817
Latest member
DicWeils

Latest Threads

Top