0

I am using javascript to set style properties on several elements when my wordpress pages are served in an iFrame. I keep getting an error and cannot figure out what I'm doing wrong. Here is what I have that works.

<script type="text/javascript">

var isInIFrame = (window.location != window.parent.location);

if(isInIFrame==true){

    document.getElementById('colophon').style.display = "none";
    document.getElementsByClassName('header-outer-wrapper')[0].style.display = "none";

}

</script>

But as soon as I add another line to my IF statement I start getting an error in the console. This is when I get an error:

<script type="text/javascript">

var isInIFrame = (window.location != window.parent.location);

if(isInIFrame==true){

    document.getElementById('colophon').style.display = "none";
    document.getElementsByClassName('header-outer-wrapper')[0].style.display = "none";
    document.getElementsByClassName('tribe-events-event-categories-label')[0].style.display = "none";

}

</script>

Here is the error I get.

TypeError: document.getElementsByClassName(...)[0] is undefined fitzgerald-high-school-college-and-career-academy-calendar:1200:225
<anonymous> Choo

I have about 12 CSS styles I need to apply based on the class. Can I not use this method multiple times in the same if statement or script? What am I doing wrong?

gacto
  • 301
  • 4
  • 10
  • Either the element doesn't exist yet, or the selector is wrong – CertainPerformance Oct 01 '19 at 00:37
  • ok i'll look at that, but i can get every element to work when i add them without any other classes. the error shows up immediately when i add the second element, no matter which element is added. so i don't know how the element couldn't exist yet being the problem. that is why i did find the answer to other questions i have reviewed relevant. – gacto Oct 01 '19 at 00:42
  • Without a [MCVE] illustrating the problem, it's impossible to say – CertainPerformance Oct 01 '19 at 00:44

0 Answers0