1

I have a DIV, that is sometimes hidden, and in this case I don't want that google adds appear/are loaded at all inside this DIV.

What is the best practice to make such a check with javascript?

2
  • When do you intend to check? Commented Apr 18, 2013 at 10:35
  • What's a google add? And how did you hide them? How do you load those adds? Commented Apr 18, 2013 at 10:53

8 Answers 8

4

You should look at the computed style of the node you want, via window.getComputedStyle, rather than the style attribute of the node, as css elsewhere may be effecting it too.

Checking whether a node is covered by another node is much more difficult, one way is to use document.elementFromPoint to find out which node is top-most at a specific point, then do this where your node should be until you're satisfied it's visible. For example, check the centre of the node is your node.

function isHidden(node, checkIfCovered) {
    var absPosition = function absPosition(node) {
            var x = 0, y = 0,
                h = node.offsetHeight || 0, w = node.offsetWidth || 0;
            do {
                node.offsetLeft && (x = x + node.offsetLeft);
                node.offsetTop && (y = y + node.offsetTop);
            } while (node = node.offsetParent);
            return {x: x, y: y, h: h, w: w};
        },
        o, style;
    if (checkIfCovered && document.elementFromPoint) { // only if supported
        o = absPosition(node); // get position & size
        o.centre = {x: o.x + o.w / 2, y: o.y + o.h / 2}; // centre of node
        if (document.elementFromPoint(o.centre.x, o.centre.y) !== node) {
            return true; // another node is in the centre => covered
        }
    }
    do { // loop up over parent nodes
        if (node.nodeType === 9) break; // skip #document
        style = window.getComputedStyle(node);
        if (   style.display === 'none'
            || style.visibility === 'hidden'
            || style.opacity === '0'
        ) {
            return true;
        }
    } while (node = node.parentNode);
    // passed all tests, not hidden
    return false;
}

Example usage

isHidden(document.getElementById('myDivId')); // true->hidden
isHidden(document.getElementById('myDivId'), true); // true->hidden or covered

Further things to consider

Sign up to request clarification or add additional context in comments.

5 Comments

unfortunately a true "isHidden" function should also check for width and height, and if some other element overlaps it.
Sounds like a pretty good answer, you could also check for opacity
Nice, I would upvote you, but I already did :D anyway beware that if the element was given e.g. transform: translate(-9999px, 0) the element is checked as visible (I suppose offsetLeft don't change)
Also after testing, visibility remains unchanged if the parent nodes are hidden, You might want to adapt your code to my pseudo code and I guess it will be hard to do better.
thanks for this saved my hours: if (node.nodeType === 9) break; // skip #document ref: w3schools.com/jsref/prop_node_nodetype.asp
2

if your div has an ID, try this:

if((document.getElementById('your_div_id').style.display=='none') || (document.getElementById('your_div_id').style.visibility=='hidden')){
//its hidden
}else{
//its not
}

1 Comment

what when visibility is hidden or if it is covered by another element with higher z-index?
1
var isVisible = element.offsetWidth > 0 || element.offsetHeight > 0;

isVisible will give you is the div hidden or visible.

Comments

0

you can check it by

var div = document.getElementById('div_id');
if( div.style.visibility=="hidden"
    || div.style.display=="none")
 {   alert("div is hidden"); }

Comments

0
<script>
function isHidden(divId){
   styleValue = document.getElementById(divId).style.display;
   if(styleValue == 'none'){
      return true;
   }
   else{
     return false;
   }
}
</script>


returnValue = isHidden(); //return true if hidden

4 Comments

I think you should see other answers first. That check is not enough
Please do not use if-statements to create boolean values. And don't forget var keywords!
Hi javascript is weakly typed in the sence there is no need to declare varible like var val = "something"; like this directly u can decalre without var for example newVal = document.getElementById('yourId');
if you don't use var all the variables you create pollute the global scope. Not a good approach
0

There are a couple of ways to know if an object is hidden or not. There are couple of ways to find out but the truth is that it's much more complicated than most solution out there.

I can't unfortunately mash it up at the moment but I can tell how to do it.

An hidden object may still have display to something different than none or visibility to default. If parent objects are hidden, then child elements are also hidden but the display attributes remain unchanged.

In other word, you'll have to bubble every parent element until you find an hidden element or the root.

That said, css attributes like display and visibility isn't the only way to hide an element. You can also check for overflow: hidden and if the object is outside of the bounding box.

Pseudo code

def visible(initial, element)
    if element.invisible or element.hidden
       // Element was simply hidden so false
       return false

    if element != initial and elemen.has_overflow_hidden && initial.outside(element)
       // Here we check if our element is inside the bounding box of a overflow hidden
       // Parent.
       return false

    if element.opacity == 0
       return false

    if element == element.parent
       // reached the root
       return true

    visible(initial, element.parent)

if (visible(element, element))
  do something

else

  do something else

As far as I can say, it's unfortunately not handling every cases. But it should be more than enough. It doesn't handle z-index, It might not work well with position absolute, relative and fixed.

Comments

-1

In jquery:

$('#div').is(':visible');
$('#div').is(':hidden');

2 Comments

What's wrong with using a library that can solve the problem faster? And I think jquery is pretty mainstream by now :)
Nothing wrong about it when the library is already included, but OP didn't specify any library in the question
-2

You're looking at the DOM elements (in this case) DIV, CSS property. There are two ways to hide an element.

One is display: none, the other is visibility: hidden.

In jQuery you would then do something like:

 if !($('#div-id').css('display') == 'none'){
    JAVASCRIPT_TO_LOAD_ADS_GOES_HERE
 }

In the case of visibility hidden, do the same, but compare .css('visibility') to hidden.

1 Comment

There's no jQuery tag on the question, so jQuery answers aren't valid. Not to mention that if you were using jQuery they already have a :visible selector to test this so doing it yourself is a huge waste of effort.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.