user1829531
user1829531

Reputation: 59

Run javascript when div shows up

I need tu run javascript (Stop JWPlayer) when DIV shows up. It shows approx. 15 sec after page is fully loaded.

The code I have so far

if (document.getElementById("overlay_div")) { jwplayer().stop(); }

i guess my code looks for overlay_div at the time of page load, and not after some time (15 sec in this case)

Edit: it turns out that DIV is actually there all the time but it switches from display: none to display:block after this time interval. Is there any way to run script whan div turns to display: block

Edit2: I managed to solve this on my own with the help of @elbunuelo Here is the code in case anyone needs it:

var interval = setInterval(function(){
 if (document.getElementById("overlay_main_div").style.display=="block"){ 
      jwplayer().stop(); 
      clearInterval(interval);
 } }, 1000);

Upvotes: 1

Views: 3024

Answers (3)

nfroidure
nfroidure

Reputation: 1601

It depends on the way the div element is shown :

-

var times=0;
function divcheck() {
    if (document.getElementById("overlay_div")) { jwplayer().stop(); }
    else if(times<10) { times++; setTimeout(divCheck,1000); }
    }

setTimeout(divCheck,15000);

Upvotes: 0

Vyacheslav Voronchuk
Vyacheslav Voronchuk

Reputation: 2463

You can add script tag just after the div, so it will run when markup is in place.

<div id="overlay_div"></div>
<script type="text/javascript">
if (document.getElementById("overlay_div")) { jwplayer().stop(); }
</script>

Upvotes: 1

elbunuelo
elbunuelo

Reputation: 91

Have you tried using an interval? i.e. Checking every 1000 ms or so if the div is present and then stopping the player?

var interval = setInterval(function(){
     if (document.getElementById("overlay_div")) { 
          jwplayer().stop(); 
          clearInterval(interval);
     }
}, 1000);

Upvotes: 0

Related Questions