Professor Zero
Professor Zero

Reputation: 35

Redirect using javascript if two conditions are met

<script src="/assets/js/ads.js" type="text/javascript"></script>
//the bait for adblocker

<script type="text/javascript">

if ((document.getElementById('ElvJCLbfcHDP')) && (window.innerWidth > 1280)){
   window.location = "/disableadblock.php";
}
else {
    do nothing
}
</script>

The ads.js has the element and if the element is blocked then I want the pc user to get redirected to the adblock disable page. If the element is present then do nothing. The requirement is that if a mobile user is blocking the ads then he should not be redirected. Only pc users should be redirected. So it should match two conditions that is check if element is not present and also check if his screen width is more than 1280.

This is my first time doing javacript and I don't know how to do it. I googled a lot but couldn't find anything.

Upvotes: 0

Views: 144

Answers (2)

Vladu Ionut
Vladu Ionut

Reputation: 8193

In case the ads is not generated synchronous you have to delay this check

setTimeout(function(){ 
var adsEl = document.getElementById('ElvJCLbfcHDP');
if (!adsEl  && window.innerWidth > 1280){
   window.location = "/disableadblock.php";
}
}, 300);

Upvotes: 0

RomanPerekhrest
RomanPerekhrest

Reputation: 92854

check if element is not present and also check if his screen width is more than 1280.

According to your requirement, it should be:

if (!document.getElementById('ElvJCLbfcHDP') && (window.innerWidth > 1280)){
   window.location = "/disableadblock.php";
}

Upvotes: 1

Related Questions