|
|
|||||||||
|
|||||||||
|
|||||||||
| |
||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
JavaScript - Can't See Object Properties
Why won't this work:
Code:
<BODY> <DIV ID="heightGuage"> </DIV> <SCRIPT LANGUAGE="javascript"> <!-- guage=document.getElementById(heightGuage) alert(guage.style.top); // --> </SCRIPT> </BODY> No alert comes up. If I use a string value in the alert statement, it works - It just doesn't see the "heightGuage" DIV's properties... joe |
|
#2
|
||||
|
||||
|
Can't See Object Properties
you don't use style.top as this to set the position you need to use offsetTop to get the value.
like the following: Code:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title></title>
</head>
<body>
<div id="heightGuage"> </div>
<script language="javascript">
<!--//
var guage = document.getElementById("heightGuage").offsetTop;
alert(guage);
// -->
</script>
</body>
</html>
__________________
Hope this advise helps. ![]() If so please show your appreciation by adding reputation points (click gauge image on top right of this post and score).
|
|
#3
|
|||
|
|||
|
Good answer - when I copy the code into a new page, it works as expected. However, when I add it to the pages it was intended for, I consistantly get a value of -1, dispite where the div appears on the page.
Here's the defective code: Code:
<HTML>
<HEAD>
<META NAME="keywords" CONTENT="SBDC,NTSBDC,SBA,small business,north texas small business development center,
small business development center, ">
<META NAME="description" CONTENT="Welcome to the North Texas Development Center - promoting small
business success by providing management education.">
<META NAME="author" CONTENT="C.J. Reynolds">
<META NAME="copyright" CONTENT="Copyright © 2005 North Texas Small Business Development Center">
<meta http-equiv="Content-Language" content="en-us">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<TITLE>North Texas Small Business Development Center - Economic Impact</TITLE>
<STYLE TYPE='TEXT/CSS'>
<!--
.head1800 {font-family:Arial Narrow, Arial, Helvetica, sans-serif, Verdana; font-style:italic; color:#000099; font-size:50px; text-align:center;}
.horGrad800 {width:330px;}
//-->
</STYLE>
<link rel="stylesheet" href="include/default.css" /> <!-- include default style sheet -->
<script type="text/javascript" language="javascript" src="include/scripts.js"></script> <!-- invoke scripts.js for javascript functions -->
</HEAD>
<BODY BGCOLOR="#80B6F1" TOPMARGIN="0" LEFTMARGIN="0" MARGINHEIGHT="0" MARGINWIDTH="0">
<DIV WIDTH="100%" BGCOLOR="WHITE" style="background-color:white; width:100%; text-align:left;"><IMG ID="topBannerImg" SRC="images/sbdc_banner2.jpg" BORDER="0" WIDTH="995" HEIGHT="139" style="position:relative; z-index:3;"></DIV>
<TABLE HEIGHT="100%" WIDTH="100%" CELLSPACING="0" CELLPADDING="0" BORDER="0" style="position:relative; top:-4;"> <!-- This is the table that contains all three columns -->
<TR>
<TD ID="tblContent" WIDTH="780" VALIGN="TOP"> <!-- open cell for right pane -->
<TABLE ALIGN="LEFT" BORDER="0" WIDTH="100%" CELLPADDING="0" CELLSPACING="0">
<TR>
<TD WIDTH="100%" ALIGN="CENTER"> <!-- All page content (the right column) will be contained in this cell -->
<!-- ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -->
<!-- ***** Page Header ***** -->
<TABLE WIDTH="100%" ALIGN="CENTER" BORDER="0" CELLPADDING="10" CELLSPACING="0">
<TR>
<TD WIDTH="100%" ALIGN="CENTER" ID="mainHeading" class="medHeading">
Economic Impact<BR>
<IMG SRC="images/horizontal_gradient.jpg" ID="hGrad" WIDTH="430" HEIGHT="13"></TD>
</TR><TR>
<!-- ***** 1st paragraph ***** -->
<TD WIDTH="100%" ALIGN="LEFT"><BR>
<DIV CLASS="firstPara">The North Texas <ACRONYM title="Small Business Development Center">SBDC</ACRONYM> Region has a population of over 7 million and more than 150,000 small businesses. To ensure comprehensive business counseling and training services are provided to our varied and large constituency, we have thirteen <A HREF="map.shtml" TARGET="_blank">field centers</A>, five specialty centers and eight satellite offices. Working synergistically, these centers have a dramatic impact on the area’s annual economy. In 2003, 1,626 jobs were created, 2,959 jobs were retained, and nearly $35 million in capital formation resulted from <ACRONYM title="Small Business Development Center">SBDC</ACRONYM> program efforts; the North Texas <ACRONYM title="Small Business Development Center">SBDCs</ACRONYM> invested 24,000 hours counseling 6,094 clients, and trained more than 13,000 entrepreneurs.</DIV>
</TD>
</TR>
</TABLE>
<DIV ID="heightGuage">This is a Test</DIV>
<SCRIPT LANGUAGE="javascript">
<!--//
var guage = document.getElementById("heightGuage").offsetTop;
alert(guage);
// -->
</SCRIPT>
<!-- ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -->
</TD>
</TR>
</TABLE> <!-- close table containing right column -->
</TD> <!-- close cell containing all page content -->
</TR>
</TABLE> <!-- close table containing page -->
</BODY>
</HTML>
If I take out two of the tables, like so: Code:
<HTML>
<HEAD>
<META NAME="keywords" CONTENT="SBDC,NTSBDC,SBA,small business,north texas small business development center,
small business development center, ">
<META NAME="description" CONTENT="Welcome to the North Texas Development Center - promoting small
business success by providing management education.">
<META NAME="author" CONTENT="C.J. Reynolds">
<META NAME="copyright" CONTENT="Copyright © 2005 North Texas Small Business Development Center">
<meta http-equiv="Content-Language" content="en-us">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<TITLE>North Texas Small Business Development Center - Economic Impact</TITLE>
<STYLE TYPE='TEXT/CSS'>
<!--
.head1800 {font-family:Arial Narrow, Arial, Helvetica, sans-serif, Verdana; font-style:italic; color:#000099; font-size:50px; text-align:center;}
.horGrad800 {width:330px;}
//-->
</STYLE>
<link rel="stylesheet" href="include/default.css" /> <!-- include default style sheet -->
<script type="text/javascript" language="javascript" src="include/scripts.js"></script> <!-- invoke scripts.js for javascript functions -->
</HEAD>
<BODY BGCOLOR="#80B6F1" TOPMARGIN="0" LEFTMARGIN="0" MARGINHEIGHT="0" MARGINWIDTH="0">
<DIV WIDTH="100%" BGCOLOR="WHITE" style="background-color:white; width:100%; text-align:left;"><IMG ID="topBannerImg" SRC="images/sbdc_banner2.jpg" BORDER="0" WIDTH="995" HEIGHT="139" style="position:relative; z-index:3;"></DIV>
<!-- ***** Page Header ***** -->
<TABLE WIDTH="100%" ALIGN="CENTER" BORDER="0" CELLPADDING="10" CELLSPACING="0">
<TR>
<TD WIDTH="100%" ALIGN="CENTER" ID="mainHeading" class="medHeading">
Economic Impact<BR>
<IMG SRC="images/horizontal_gradient.jpg" ID="hGrad" WIDTH="430" HEIGHT="13"></TD>
</TR><TR>
<!-- ***** 1st paragraph ***** -->
<TD WIDTH="100%" ALIGN="LEFT"><BR>
<DIV CLASS="firstPara">The North Texas <ACRONYM title="Small Business Development Center">SBDC</ACRONYM> Region has a population of over 7 million and more than 150,000 small businesses. To ensure comprehensive business counseling and training services are provided to our varied and large constituency, we have thirteen <A HREF="map.shtml" TARGET="_blank">field centers</A>, five specialty centers and eight satellite offices. Working synergistically, these centers have a dramatic impact on the area’s annual economy. In 2003, 1,626 jobs were created, 2,959 jobs were retained, and nearly $35 million in capital formation resulted from <ACRONYM title="Small Business Development Center">SBDC</ACRONYM> program efforts; the North Texas <ACRONYM title="Small Business Development Center">SBDCs</ACRONYM> invested 24,000 hours counseling 6,094 clients, and trained more than 13,000 entrepreneurs.</DIV>
</TD>
</TR>
</TABLE>
<DIV ID="heightGuage">This is a Test</DIV>
<SCRIPT LANGUAGE="javascript">
<!--//
var guage = document.getElementById("heightGuage").offsetTop;
alert(guage);
// -->
</SCRIPT>
</BODY>
</HTML>
Then it works. Any suggestions? joe Last edited by cjreynolds : April 8th, 2008 at 02:26 PM. |
|
#4
|
|||
|
|||
|
I seem to have the problem identified (not solved)...
If I put the "heightGuage" div inside any table, it doesn't work. This works: Code:
<DIV ID="heightGuage">This is a Test</DIV>
<SCRIPT LANGUAGE="javascript">
<!--//
var guage = document.getElementById("heightGuage").offsetTop;
alert(guage);
// -->
</SCRIPT>
</BODY>
</HTML>
This doesn't: Code:
<TABLE><TR><TD>
<DIV ID="heightGuage">This is a Test</DIV>
<SCRIPT LANGUAGE="javascript">
<!--//
var guage = document.getElementById("heightGuage").offsetTop;
alert(guage);
// -->
</SCRIPT>
</TD></TD></TABLE>
</BODY>
</HTML>
It returns "0" Hope that helps... joe |
|
#5
|
|||
|
|||
|
Found a fix!
Of course, I just realized that the code where I need to detect the top of the div is loaded before the div is built, so there's no way I can do what I set out to do - Oh, well... Anyway, if anyone needs it: Code:
<HTML>
<HEAD>
<SCRIPT LANGUAGE="javascript">
<!--//
function getElementTopPosition(e){
var y=0;
while(e){
y+=e.offsetTop+e.clientTop;
e=e.offsetParent;
}
//return y;
alert(y);
}
// -->
</SCRIPT>
</HEAD>
<BODY>
<TABLE>
<TR>
<TD>
<DIV ID="heightGuage">This is a Test</DIV>
</TD>
</TR>
</TABLE>
<SCRIPT LANGUAGE="javascript">
<!--
getElementTopPosition(heightGuage);
//-->
</SCRIPT>
</BODY>
</HTML>
Thanks (as always) for the support, but no cigar this time... Y'All have a great day! joe |
|
#6
|
|||
|
|||
|
I know this is a little late.. maybe you've got email notifications on. anyway..
Take this out: Code:
<SCRIPT LANGUAGE="javascript"> <!-- getElementTopPosition(heightGuage); //--> </SCRIPT> Do this: Code:
<body onload="getElementTopPosition(heightGauge)"> you mentioned it didn't work since the code was run before the div was created.. putting a function in the onload section triggers it once the page is finished ^.~ problem solved. alternatively, if you have more things you'd like to do upon the page loading.. make a load() function, then do what you want inside that function. Quote:
|
![]() |
| Viewing: ASP Free Forums > Programming > HTML, JavaScript And CSS Help > JavaScript - Can't See Object Properties |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|