Javascript Document Height

help make equivalent javascript code for jquery code below

  <script>
var httmp=parseInt($(document).height());
var htorg   = parseInt(httmp-71)
$("div.content_box").height( htorg);
    </script>
+3
source share
1 answer

Here is my attempt to get javascript code for this:

// 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 the same class, you should modify this loop as follows:

// 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';
  }
}

Now you need to install heightall the elements that have a class content_box.

+2
source

Source: https://habr.com/ru/post/1793219/


All Articles