views:

1456

answers:

6

hi hi guys, I'm pretty confused! with this:

...
<div id="main">   
    <div id="content">
        <div class="col1">
     ...COLUMN1 CONTENT GOES HERE...
        </div>

        <div class="col2">
     ...COLUMN2 CONTENT GOES HERE...
        </div>
    </div><!-- #content -->
</div><!-- #main -->
...

there are columns as you see, and I want to set their container element's height to the maximum size of both columns(plus 130px). so by using Prototype framework:

//fixing column height problem
Event.observe(window,"load",function(){    
    if(parseInt($('col1').getStyle('height')) > parseInt($('col2').getStyle('height')))
        $('main').setStyle({'height' : parseInt($('col1').getStyle('height'))+130+'px'});
    else
        $('main').setStyle({'height' : parseInt($('col2').getStyle('height'))+130+'px'});
});//observe

It working nice in Firefox, Opera, Safari & Chrome but it fails to return the actual height of columns. in IE7+ (not tested in IE6) it returns NaN as columns height.
I've managed to find out that's because of this:

.col1,.col2{"height:auto;"}

I've also used "$('col1').offsetHeight" and it's returning 0 as the height value of each column.

the HTML is styled in this way:

#main{
height: 455px;
background: #484848 url(../images/mainbg.png) repeat-x;
}
#content{
/*height:80%;*/
width: 960px;
direction: rtl;
margin-top: 50px;
margin-left: auto;
margin-right: auto;
position: relative;
}
.col1,.col2{
width: 33%;
text-align: right;
margin-left:3px;
padding-right:3px;
line-height:17px;
}
.col1{padding-top:20px;}
.col1 ul{
margin:0;
padding:0;
list-style: url(../images/listBullet.gif);
}
.col1 ul li{
margin-bottom:20px;
}
.col2{
top: 0;
right: 70%;
position: absolute;
}

any idea on the issue please?!

update/ It tooks three days to solve, and I was at the very risk of making a bounty!
for the solution please take a look at this question/answer.

A: 

Artarad,
The above style i.e. css shows that you have not assigned height. Please try adding _height:auto in class which will look like

    .col1,.col2{
width: 33%;
_height:auto;
height:auto;
text-align: right;
margin-left:3px;
padding-right:3px;
line-height:17px;
}

Basically, many browsers fail to recognize the height when assigned to a particular div or any element . In this case we use the above work arounds. I hope the above helps you.

Thanks,
Samiksha

Samiksha
many thanks Samiksha, but It's not working for me. Additionally I removed the 80% height rule and it's keep failing :( actually what this underline character shall be able to do?
artarad
The underscore used before any attribute signifies that it applies to ie7 browser only. I apologize for this solution not helped you.. will let you know definitely if i find a way...
Samiksha
A: 

I don't know if it will work (without testing), but you could try jQuery and height(); this (in theory) gives the computed height. Worth a try...

Marc Gravell
thanks Marc, but I'm using prototype and I think that won't be nice to use both of 'em on a single page, ain't it?
artarad
+4  A: 

As a completion for Marc's answer; There's an equal for jQuery's height() in Prototype:

$('col1').getDimensions().height //or .width ofcourse

And here is its documentation: http://prototypejs.org/api/element/getDimensions

update: I do agree with crescentfresh's answer below. Since I had the absolute same problem in the past, I've searched all possible methods to find the dimension properties but I failed as you will. please take a look at this:

function getStyle(oElm, strCssRule){
    var strValue = "";
    if(document.defaultView && document.defaultView.getComputedStyle){
     strValue = document.defaultView.getComputedStyle(oElm, "").getPropertyValue(strCssRule);
    }
    else if(oElm.currentStyle){
     strCssRule = strCssRule.replace(/\-(\w)/g, function (strMatch, p1){
      return p1.toUpperCase();
     });
     strValue = oElm.currentStyle[strCssRule];
    }
    return strValue;
}

As you see, the function has been written to get the computed rendered current style of an element, but in our case even this method will fail, I think. (worth a try)
So, as crescentfresh said, you have to find the problem in your CSS positioning method while not wasting your time seeking for a proper javascript function which could be able to do the magic. let's begin by removing that #content DIV and letting the #main to be the only wrapper of said columns, and then styling the remain to achieve the desired goal.

Sepehr Lajevardi
Looks like a winner ;-p
Marc Gravell
thank you both, but it's not working!!! returning 0 as before. I'm extremely weired through this -_-
artarad
any other suggestion? :(
artarad
I'm absolutely confused but I really appreciate the help. many thanks :)
artarad
+1  A: 

From: http://stackoverflow.com/questions/636474/why-would-jquery-return-0-for-an-offsetheight-when-firebug-says-its-34

An element that is not actually taking part in the document render process has no dimensions, and will give an offsetWidth/Height of 0.

Both Prototype's getDimensions() and jQuery's height() read the offsetHeight or clientHeight properties, which you've tried and got 0. So somewhere in your code there must be something taking #col out of the rendering flow. That's all I can think of.

Crescent Fresh
follow up: http://is.gd/pour
Sepehr Lajevardi
I'm going to try, thanks ^-^
artarad
A: 

Since IE wants to give you a hard time, you can give it some special attention and use a property that I believe it will recognize...

var height;
if(document.all) { //This means it is IE
  height = document.getElementById('col1').offsetHeight;
}
else {
  height = //Use what is working in other browsers now
}
Josh Stodola
Hi Josh, the offsetHeight() is not working and I've mentioned that in the question body, thanks ;)
artarad
I seen what you mentioned in the question body. But you are using the $() function instead of the native getElementById(), and I am not exactly sure what that returns.
Josh Stodola
A: 

I know this is an old question but I found a better answer than "Fix the css and forget the javascript".

I had the exast same problem and used .innerHeight() and it forced jQuery to calculate the height. For some reason .outerHeight() did not work whilst .innerHeight() solved the problem. Not sure if there is a similar method for prototype. You could look into the jquery library and figure out how .innerHeight() works, and write your own.

Dale