Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Document Height in Javascript

help to make an equivalent Javascript code for the Jquery code below

  <script>
var httmp=parseInt($(document).height());
var htorg   = parseInt(httmp-71)
$("div.content_box").height( htorg);
    </script>
like image 947
Sujith Kumar KS Avatar asked Sep 12 '26 19:09

Sujith Kumar KS


1 Answers

Here is my attempt to get javascript code for that:

// Cross browser function to get document height
// http://james.padolsey.com/javascript/get-document-height-cross-browser/
function getDocHeight() {
    var D = document;
    return Math.max(
        Math.max(D.body.scrollHeight, D.documentElement.scrollHeight),
        Math.max(D.body.offsetHeight, D.documentElement.offsetHeight),
        Math.max(D.body.clientHeight, D.documentElement.clientHeight)
    );
}

var httmp = getDocHeight();
var htorg = parseInt(httmp - 71);

var elms = document.getElementsByTagName('div');
var theDiv = null;

// search for element with class content_box
for (var i = 0; i < elms.length; i++){
  if (elms[i].getAttribute('class') === 'content_box'){
    theDiv = elms[i];
    break;
  }
}

theDiv.style.height = htorg + 'px';

If you have multiple elements with same class, you should modify the loop like this:

// search for element with class content_box
for (var i = 0; i < elms.length; i++){
  if (elms[i].getAttribute('class') === 'content_box'){
    theDiv = elms[i];
    theDiv.style.height = htorg + 'px';
  }
}

That should now set the height of all elements that have class content_box.

like image 98
Sarfraz Avatar answered Sep 15 '26 10:09

Sarfraz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!