javascript - 如何根据引荐流量隐藏页面上的某些元素?

标签 javascript

更具体地说,如何隐藏广告?阅读本文后我提出这个问题: coding horror entry

他在里面说

As a courtesy, turn off ads for Digg, Reddit, and other popular referring URLs. This audience doesn't appreciate ads, and they're the least likely to click them anyway.

我同意他的说法。那么我该怎么做呢?

最佳答案

我会为此使用 PHP,因为用于隐藏广告的 JavaScript 代码会让您看起来像是在为所有人隐藏广告并从中获取 yield (Google 很聪明,所以他们会发现您做了某事像那样)。

但是,使用 PHP,您可以在页面到达用户之前对其进行修改,从而消除该问题。基本上,您有条件地检查浏览器的来源:

<?php
  $sites = array("reddit.com", "digg.com");

  if (!in_array(parse_url($_SERVER['HTTP_REFERER'], PHP_URL_HOST), $sites)) :
?>

  <div>your ads</div>

<?php else:?>

  <div>Hello reddit person</div>

<?php endif; ?>

您必须让您的网站运行 PHP 代码(它将是动态的)以有条件地展示您的广告。但是,此代码不起作用,因为 reddit 不是 URL,但您明白了;检查 reddit.com 的 URL。

关于javascript - 如何根据引荐流量隐藏页面上的某些元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6659375/

相关文章:

javascript - ReactJS:如何为sortableJS动态设置禁用选项?

javascript - 从 Graph 获取单个消息

javascript - 如果您要查找的元素不存在,如何不破坏 Cypress 测试

javascript - 了解 Cloud Firestore 计费

javascript - 为什么 Object._proto_ 引用 Function.prototype?

javascript - 标记或圆圈在 Leaflet 上不起作用

javascript - Kendo UI 自动完成、名称搜索

javascript - javascript书签代码中 'scr' +'ipt'的串联

javascript - 减少 Javascript 中垃圾收集器事件的最佳实践

javascript - 错误 TypeError : Cannot read property 'subscribeNewsletter' of undefined at newsletter. component.ts.NewsletterComponent.subscribeNewsletter