简体   繁体   English

通过引荐访问网站来隐藏“ AdSense” div”

[英]Hiding `AdSense` `div` by accessing site from referral

I'm really a newbie to everything around programming ok. 我真的是编程方面的新手。 So... 所以...

I want to hide an AdSense div when accessing from a specific source (for example: ?utm_source=www.test.com) for the time a user is still moving around on my site or 120 seconds after that. 当用户仍在我的网站上移动时或之后的120秒内,我想在从特定来源(例如: ?utm_source=www.test.com)访问时隐藏AdSense div

PS: I'm in a partnership with another advertising company and I have to show their ads when visitors come through this referral. PS:我与另一家广告公司建立了合作伙伴关系,当访问者通过此引荐获得访问时,我必须展示他们的广告。

I have an AdSense code: 我有一个AdSense代码:

<div class="td-all-devices">
<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- Test -->
<ins class="adsbygoogle"
     style="display:inline-block;width:300px;height:600px"
     data-ad-client="ca-pub-0000000000000"
     data-ad-slot="000000"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
</div>

I tried this, but AdSense keep showing in footer : 我尝试了此方法,但AdSense始终显示在footer

function getQueryVariable(variable)
{ 
  var query = window.location.search.substring(1); 
  var vars = query.split("&"); 
  for (var i=0;i<vars.length;i++)
  { 
    var pair = vars[i].split("="); 
    if (pair[0] == variable)
    { 
      return pair[1]; 
    } 
  }
  return -1; //not found 
}

if ( getQueryVariable('utm_source') == 'www.test.com' )
{
    im.getAds(pozice);
    var ads = 1;

    document.getElementsByClassName("td-all-devices").style.display = 'none';
}

else 
{
    var ads = 0;
}

Can anyone help? 有人可以帮忙吗?

In here: 在这里:

if ( getQueryVariable('utm_source') == 'www.test.com' )
{
    im.getAds(pozice);
    var ads = 1;
    document.getElementsByClassName("td-all-devices").style.display = 'none';
}

document.getElementsByClassName returns an array of objects. document.getElementsByClassName返回一个对象数组。 Therefore you need to iterate through it to change the value of something in it. 因此,您需要遍历它以更改其中某些内容的价值。 Well, you don't HAVE TO ITERATE through it... as long as you can specify which index of the collection you want to play with. 好吧,您不必通过它进行迭代……只要您可以指定要播放的收藏夹的哪个索引即可。

Something like this: 像这样:

if ( getQueryVariable('utm_source') == 'www.test.com' )
{
    im.getAds(pozice);
    var ads = 1;
    var theElements = document.getElementsByClassName("td-all-devices");
    for(var i = 0; i < theElements.length; i++)
    {
    theElements[i].style.display = 'none';
    }
}

else 
{
    var ads = 0;
}

document.getElementById() returns 1 element you could have modified using this document.getElementById("whatever unique id").style.display = 'none'; document.getElementById()返回1个您可以使用document.getElementById("whatever unique id").style.display = 'none';修改的元素document.getElementById("whatever unique id").style.display = 'none';

document.getElement s ByClassName() returns a collection of element s you could have modified like this document.getElementsByClassName("whatever class name")[index position].style.display = 'none'; document.getElement ByClassName()返回您可以像这样修改过的element 集合。getElementsByClassName( document.getElementsByClassName("whatever class name")[index position].style.display = 'none';

Since you are a self proclaimed newbie, I will invite you to discover the many Developer tools your preferred browser likely has to offer you. 由于您是个自称是新手的人,因此我将邀请您发现您喜欢的浏览器可能提供的许多开发人员工具。 Most browsers have a console you can look at. 大多数浏览器都有您可以查看的控制台。 The console will help you tremendously in troubleshooting your code. 该控制台将极大地帮助您排除代码故障。 In console, you would have picked your problem almost instantly because you would have seen an entry like this: 在控制台中,您几乎会立即选择问题,因为您将看到如下所示的条目:

TypeError: document.getElementsByClassName(...).style is undefined TypeError:document.getElementsByClassName(...)。style未定义

In Firefox, with unmodified keyboard shortcuts, you can bring up the developers palette using ctrl + shift + s . 在Firefox中,使用未修改的键盘快捷键,您可以使用ctrl + shift + s调出开发人员面板。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM