473,587 Members | 2,451 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

DOCTYPE detector

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.doctyp e 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.dt d">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitl ed Document</title>
</head>

<body>
<script language="JavaS cript" type="text/javascript">
//document.getEle mentsByTagName( '!DOCTYPE') generates an "object", but what
type? element? if so, why won't element properties work?
document.write( document.doctyp e); //prints nothing
document.write( document.getEle mentsByTagName( 'html').getAttr ibute("xmlns")) ;
//prints nothing
document.write( document.getEle mentsByTagName( '!DOCTYPE').has Attribute("-//W3C//DTD
XHTML 1.0 Transitional//EN")); //prints nothing
//if (document.getEl ementsByName('! DOCTYPE') != null ||
document.getEle mentsByName('ht ml').getAttribu te('xmlns') != null) {
// document.write( document.getEle mentsByName('ht ml').innerHTML) ;
//}
</script>

</body>
</html>

I'm really frustrated. I lack info.

Jim Michaels
Aug 5 '06 #1
24 9158
Jim Michaels wrote:
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.doctyp e 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.dt d">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitl ed Document</title>
</head>

<body>
<script language="JavaS cript" type="text/javascript">
//document.getEle mentsByTagName( '!DOCTYPE') generates an "object", but what
type? element? if so, why won't element properties work?
document.write( document.doctyp e); //prints nothing
document.write( document.getEle mentsByTagName( 'html').getAttr ibute("xmlns")) ;
//prints nothing
document.write( document.getEle mentsByTagName( '!DOCTYPE').has Attribute("-//W3C//DTD
XHTML 1.0 Transitional//EN")); //prints nothing
//if (document.getEl ementsByName('! DOCTYPE') != null ||
document.getEle mentsByName('ht ml').getAttribu te('xmlns') != null) {
// document.write( document.getEle mentsByName('ht ml').innerHTML) ;
//}
</script>

</body>
</html>

I'm really frustrated. I lack info.
Why do you need to know it, first of all?
Jim Michaels
Aug 5 '06 #2
Jim Michaels wrote:
>I can't get any "universal" code working that tries to detect whether the
document it's in is xhtml or html.
Why do you need to know it, first of all?
because generated singleton elements are different, such as <bror <br />
depending on whether it's html or xhtml.
Aug 5 '06 #3
Jim Michaels wrote:
Jim Michaels wrote:
I can't get any "universal" code working that tries to detect whether the
document it's in is xhtml or html.
Why do you need to know it, first of all?

because generated singleton elements are different, such as <bror <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.

Aug 5 '06 #4
Jim Michaels said the following on 8/5/2006 1:41 AM:
>Jim Michaels wrote:
>>I can't get any "universal" code working that tries to detect whether the
document it's in is xhtml or html.
>Why do you need to know it, first of all?

because generated singleton elements are different, such as <bror <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.

--
Randy
comp.lang.javas cript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Aug 5 '06 #5

"darwinist" <da*******@gmai l.comwrote in message
news:11******** **************@ m73g2000cwd.goo glegroups.com.. .
Jim Michaels wrote:
Jim Michaels wrote:
I can't get any "universal" code working that tries to detect whether
the
document it's in is xhtml or html.
Why do you need to know it, first of all?

because generated singleton elements are different, such as <bror <br
/>
depending on whether it's html or xhtml.
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 <brwhich 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.
Aug 5 '06 #6
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=n ew versionInfo();
/*************** *************** ***************
Just check for internet explorer.
*************** *************** *************** */
if(typeof document.namesp aces != "undefined" ){
if(document.all[0].nodeType==8)
re.exec(documen t.all[0].nodeValue);
else
return null;
}else{
if(document.doc type != null)
re.exec(documen t.doctype.publi cId);
else
return null;
}
myversionInfo.x html=RegExp.$1;
myversionInfo.v ersion=RegExp.$ 2;
myversionInfo.i mportance=RegEx p.$3;
return myversionInfo;
}
var myversionInfo=d etectDoctype();
if(myversionInf o != null){
alert(myversion Info.xhtml);
alert(myversion Info.version);
alert(myversion Info.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.doctyp e 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.dt d">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitl ed Document</title>
</head>

<body>
<script language="JavaS cript" type="text/javascript">
//document.getEle mentsByTagName( '!DOCTYPE') generates an "object", but what
type? element? if so, why won't element properties work?
document.write( document.doctyp e); //prints nothing
document.write( document.getEle mentsByTagName( 'html').getAttr ibute("xmlns")) ;
//prints nothing
document.write( document.getEle mentsByTagName( '!DOCTYPE').has Attribute("-//W3C//DTD
XHTML 1.0 Transitional//EN")); //prints nothing
//if (document.getEl ementsByName('! DOCTYPE') != null ||
document.getEle mentsByName('ht ml').getAttribu te('xmlns') != null) {
// document.write( document.getEle mentsByName('ht ml').innerHTML) ;
//}
</script>

</body>
</html>

I'm really frustrated. I lack info.

Jim Michaels
Aug 5 '06 #7
"Jim Michaels" <NO************ ***@yahoo.comwr ote in
news:bO******** *************** *******@comcast .com:
real simple. things like:
if (isXhtml) {document.write ('<br />');} else
{document.write ('<br>');}

xhtml affects things like singleton elements/tags like <brwhich
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.
Aug 5 '06 #8


Jim Michaels wrote:
"darwinist" <da*******@gmai l.comwrote in message
>>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 <brwhich 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.
--

Martin Honnen
http://JavaScript.FAQTs.com/
Aug 5 '06 #9
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.namesp aces != "undefined" )
res=document.al l[0].nodeType==8 ?
re.test(documen t.all[0].nodeValue) : false;
else
res=document.do ctype != null ?
re.test(documen t.doctype.publi cId) : 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=d etectDoctype();
if(myversionInf o != null){
alert(myversion Info.xhtml);
alert(myversion Info.version);
alert(myversion Info.importance );
}
else{
alert("There is no DOCTYPE in the code!");
}
go******@gmail. com написа:
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=n ew versionInfo();
/*************** *************** ***************
Just check for internet explorer.
*************** *************** *************** */
if(typeof document.namesp aces != "undefined" ){
if(document.all[0].nodeType==8)
re.exec(documen t.all[0].nodeValue);
else
return null;
}else{
if(document.doc type != null)
re.exec(documen t.doctype.publi cId);
else
return null;
}
myversionInfo.x html=RegExp.$1;
myversionInfo.v ersion=RegExp.$ 2;
myversionInfo.i mportance=RegEx p.$3;
return myversionInfo;
}
var myversionInfo=d etectDoctype();
if(myversionInf o != null){
alert(myversion Info.xhtml);
alert(myversion Info.version);
alert(myversion Info.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.doctyp e 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.dt d">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitl ed Document</title>
</head>

<body>
<script language="JavaS cript" type="text/javascript">
//document.getEle mentsByTagName( '!DOCTYPE') generates an "object", but what
type? element? if so, why won't element properties work?
document.write( document.doctyp e); //prints nothing
document.write( document.getEle mentsByTagName( 'html').getAttr ibute("xmlns")) ;
//prints nothing
document.write( document.getEle mentsByTagName( '!DOCTYPE').has Attribute("-//W3C//DTD
XHTML 1.0 Transitional//EN")); //prints nothing
//if (document.getEl ementsByName('! DOCTYPE') != null ||
document.getEle mentsByName('ht ml').getAttribu te('xmlns') != null) {
// document.write( document.getEle mentsByName('ht ml').innerHTML) ;
//}
</script>

</body>
</html>

I'm really frustrated. I lack info.

Jim Michaels
Aug 5 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2319
by: Joh | last post by:
Hello, (sorry long) i think i have missed something in the code below, i would like to design some kind of detector with python, but i feel totally in a no way now and need some advices to advance :( data = "it is an <atag> example of the kind of </atag> data it must handle and another kind of data".split(" ")
39
2881
by: Holly | last post by:
I'm trying to validate my code and I can't figure out what kind of doctype I have. The validator can't tell me anything because it can't move beyond the doctype declaration. http://www.wavian.com/clients/pugwash/ Is there anyway to tell what kind of doctype this is? I tried inserting a few different types (please excuse me if this is the...
43
5562
by: Gremlin | last post by:
If you are not familiar with the halting problem, I will not go into it in detail but it states that it is impossible to write a program that can tell if a loop is infinite or not. This is a fallacy built on the assumption of mythical infinite all powerfull machines. In reality we deal with finite machines that are capable of two states in...
4
2368
by: Chinmoy Mukherjee | last post by:
Hi All, Do you know of any free memory leak detector for C++ for windows OS? Regards, Chinmoy
2
1947
by: Leon Lambert | last post by:
I was wondering if someone could post some links to some bug pattern detector software for C#. I found this very interesting article about pattern detectors for Java so wanted to investiage one for C#. http://www.javaworld.com/javaworld/jw-11-2003/jw-1121-quality.html? I did a google search for variations of the following "C# Bug Pattern...
1
2061
by: Lighter | last post by:
Is there a way to write a memory leak detector supporting new(nothrow)? For example, #include <My_Debug_New.h> using namespace std; int main() {
0
2202
by: Dmitriy V'jukov | last post by:
I want to announce release 1.1 of Relacy Race Detector. First of all, now you can freely DOWNLOAD latest version of Relacy Race Detector DIRECTLY FROM WEB: http://groups.google.com/group/relacy/files Main change in release 1.1 is support for standard synchronization primitives: 1. mutex (std::mutex, pthread_mutex_init,...
0
7920
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main...
0
8215
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8347
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
8220
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6626
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5394
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3844
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2358
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1189
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.