Any help is appreciated.
Required
========
A method to detect the width and height values of a
<DIV... element. Since there are two ways to "style"
a DIV, it should be noted that a CSS file is used in
this case. The same is required for an <IMG... element
contained within the DIV.
SOLUTION
========
1. ???
Specification
=============
A file, INDEX.HTML, exists and is loaded into a browser.
The browser is version 4 or better, NN or IE (OS=Win32).
A file, TEST.CSS, provides a class for the DIV.
Using JavaScript, establish the actual WIDTH and HEIGHT
of the DIV declared in the html code. The same applies
for the IMG contained in the DIV.
These values are to be stored in two global variables
(as pixel counts) that may be accessed by scripts later
in the code.
Specifically, the structuring of the content will depend
on these values so placing the function call in <BODY onload..>
(which executes on completion of loading) is unacceptable
since the page may be "jumbled" until fully loaded.
TEST.CSS
========
Expand|Select|Wrap|Line Numbers
- DivImg {
- position: absolute;
- visibility: visible;
- z-index: auto;
- overflow: hidden;
- top: 0px;
- left: 0px;
- width: 16px;
- height: 16px;
- right: auto;
- bottom: auto;
- clip: rect(auto auto auto auto);
- margin: 4px;
- padding: 4px;
- border: 2px solid #0000FF;
- background-color: #00FFFF;
- background-image: none;
- background-repeat: no-repeat;
- background-position: center center;
- background-attachment: fixed;
- }
==============
Expand|Select|Wrap|Line Numbers
- <HTML>
- <HEAD>
- <title>INDEX</title>
- <link href="TEST.CSS" rel="stylesheet" type="text/css">
- <script language="JavaScript">
- <!-- Local Code
- DivWid = -1;
- DivHgt = -1;
- function GetDivDim(DivNameID) {
- var DivRef = null;
- if (BrowserTyp == "NN") {
- DivRef = document.getElementsByName(DivNameID) ???;
- if (DivRef != null) {
- DivWid = DivRef.width ???;
- DivHgt = DivRef.height ???;
- alert('NN:GetDivDim - '+DivWid+' x '+DivHgt);
- } else alert('NN:GetDivDim - DivRef is NULL');
- }
- if (BrowserTyp == "IE") {
- DivRef = document.getElementsByName(DivNameID) ???;
- if (DivRef != null) {
- DivWid = DivRef.width ???;
- DivHgt = DivRef.height ???;
- alert('IE:GetDivDim - '+DivWid+' x '+DivHgt);
- } else alert('IE:GetDivDim - DivRef is NULL');
- }
- }
- ImgWid = -1;
- ImgHgt = -1;
- function GetImgDim(ImgName) {
- var ImgRef = null;
- if (BrowserTyp == "NN") {
- ImgRef = document[ImgName] ???;
- if (ImgRef != null) {
- ImgWid = ImgRef.width ???;
- ImgHgt = ImgRef.height ???;
- alert('NN:GetImgDim - '+ImgWid+' x '+ImgHgt);
- } else alert('NN:GetImgDim - ImgRef is NULL');
- }
- // This part functions correctly
- if (BrowserTyp == "IE") {
- ImgRef = document[ImgName];
- if (ImgRef != null) {
- ImgWid = ImgRef.width;
- ImgHgt = ImgRef.height;
- }
- }
- }
- // -->
- </script>
- </HEAD>
- <BODY>
- <DIV ID="LogoDiv" CLASS="DivImg">
- <IMG NAME="LogoImg" src="Logo.JPG" alt="LOGO IMAGE" border="0">
- </DIV>
- <script>
- GetDivDim('LogoDiv');
- GetDivDim('LogoImg');
- </script>
- <TABLE border="1">
- <TR>
- <TD align="center"> DIV WxH </TD>
- <TD align="center"><script>document.write(DivWid+' x '+DivHgt);</script></TD>
- </TR>
- <TR>
- <TD align="center"> IMG WxH </TD>
- <TD align="center"><script>document.write(ImgWid+' x '+ImgHgt);</script></TD>
- </TR>
- </TABLE>
- <!-- etc. etc. etc. -->
- </BODY>
- </HTML>
============
The GetDivDim() and GetImgDim() functions will later be tested in
NN:4.7, NN:6.2.3 and IE:5.0 under WinNT:4.0, but for the moment
NN:6.2.3 and IE:6.0 under WinNT:5.1(XP) were used.
It is expected that the DIV will behave as specified in TEST.CSS
and therefore at least have some detectable dimensions. Of course,
allowing it to interact with it's content, the IMG (for which the
absolute dimensions are verifiable), is expected to change it's
dimensions. Detecting those dimensions, whatever they are, seems
to be failing.
First, is ID="LogoDiv" a better option than NAME="LogoDiv" or are
the two equivalent? A report that the latter is more stable has
been mentioned in some (forgotten) article.
Second, it seems there is a problem establishing how DivRef and
ImgRef should be defined (ascribe this to inexperience and/or
lack of proper documentation).
GetDivDim():
- In cases where document.getElementsByName() is used (with
NAME=), DivRef is not null but .width and .height are both
reported as "undefined".
- The same is true when document.getElementById() is used
(with ID=).
- If document.all.DivNameID is used (in IE with either ID=
or NAME=) then DivRef is null.
GetImgDim():
It seems that ImgRef is valid for NN but values of 0 are reported
for W and H.
The code operates correctly for IE.
Conclusion
==========
???