Hi All,
This is the parent issue that led to the post Need help with getDOCTYPE() function (http://www.daniweb.com/forums/thread196837.html)
USING THE BODY METHOD: The following code will properly get the viewable window area if no DOCTYPE is specified. document.getElementsByTagName("body")[0].clientWidth//Height
OR document.body.clientWidth//Height
If DOCTYPE is XHTML ANY* the above code reports only the defined page area as if it was a wraper**.
USING THE HTML METHOD: The following code will properly get the viewable window area when DOCTYPE XHTML ANY* is used. document.getElementsByTagName("html")[0].clientWidth//Height
OR document.documentElement.clientWidth//Height
If no DOCTYPE is used the above code reports only the defined page area as if it was a wrapper**, EXCEPT IE6+ (not tested in older browsers) reports 0.
-----
So the problem here is how to create a cross-doctype compatible function for getting the width & height of the window/frame. I have played with several methods but each produced problems. The best solution I can think of is to detect the doctype by using the following code:
FireFox3, Safari and Chrome*** work according to the W3C standard document.doctype
using .publicId
& .systemId
, but this causes IE to get a hard error. IE will however allow reference to the DOCTYPE through node objects. Both document.body.parentNode.parentNode.firstChild.nodeValue
and document.getElementsByTagName("!")[0].nodeValue
work in IE but not other browsers. So I came up with a function to tackle this issue:
function getDOCTYPE(){
var pid,sid,val,lng,ver,typ;
try{
pid=String(document.doctype.publicId);
sid=String(document.doctype.systemId);
}
catch(err){
val=String(document.body.parentNode.parentNode.firstChild.nodeValue)
pid=val.replace(/^[^\"]*\"([^\"]+)\".*/,"$1");
sid=((/http/).test(val))?val.replace(/^.*\"\s\"(http.*)/,"$1"):null
}
lng=((/xhtml/i).test(pid+sid))?"XHTML":"HTML";
typ=((/strict/i).test(pid+sid))?"Strict":((/trans|loose/i).test(pid+sid))?"Transitional":((/frame/i).test(pid+sid))?"Frameset":((/basic/i).test(pid+sid))?"Basic":((/mobile/i).test(pid+sid))?"Mobile":null;
ver=((/html\s*\d+\.?\d*/i).test(pid))?pid.replace(/^.*html\s*(\d+\.?\d*)\D*/i,"$1"):null;
return {publicId:pid,systemId:sid,language:lng,type:typ,version:ver,set:lng+" "+typ+" "+ver}
}
As the page must be fully rendered before the function can be implemented be sure to call the function through an event such as:
window.onload=function(){
var dtd=getDOCTYPE();
//as noted in the function, the following variables can be called: publicId, systemId, language, type, version and set
alert(dtd.set)
}
THE PROBLEM!: I am attempting to provide support for the Opera browser and neither method from above will provide info on the DOCTYPE. So I either need a solution to the original clientWidth/Height issue or find a way to get the DOCTYPE info in Opera.
Thank you in advance on any assistance provided! :)
~MikeO
FOOT NOTES:
*Strict, Transitional, etc.
**if a <div style="width:300px;height:100;"></div>
is used (and no positioning is set) as the only object in the body then body reports 300px/100px reguardless of how large the browser window is sized to. if positioning is set then the body area becomes 0px/0px (as the object <div></div>
has been moved out of the wrapper). This can be restored by setting the position of the body to relative.
***Google Chrome only works if <meta name="content-type" content="application/xhtml+xml; charset=iso-8859-1" />
is used instead of the more commonly used <meta name="content-type" content="text/html; charset=iso-8859-1" />
. However, this enforces a very strict code policy and will typically cause older or improperly formatted pages to be rendered with undesired results. Personally, I prefer to design all pages with this meta.