11
2019
12

js实现每个IP每天只出现一次的弹窗广告页

弹窗广告虽然效果很好,但很容易招人反感,如何把握用户体验与广告效果的平衡就很值得重视,下面推荐一段限制每IP只弹出一次的弹窗广告代码,较好了解决了用户体验的问题。

预览页面以直接中转到广告页面代码为例

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
		<script src="../js/jquery-1.11.3.min.js" type="text/javascript"></script>
		<script type="text/javascript">
function cookieGO(name) {
  var today = new Date();
  var expires = new Date();
  expires.setTime(today.getTime() + 1000*60*60*24);
  setCookie("cookievalue", name, expires);
}

function setCookie(name, value, expire) {   
  window.document.cookie = name + "=" + escape(value) + ((expire == null) ? "" : ("; expires=" + expire.toGMTString()));
}

function getCookie(Name) {   
   var findcookie = Name + "=";
   if (window.document.cookie.length > 0) { // if there are any cookies
     offset = window.document.cookie.indexOf(findcookie);
  if (offset != -1) { // if cookie exists
       offset += findcookie.length;          // set index of beginning of value
    end = window.document.cookie.indexOf(";", offset)          // set index of end of cookie value
    if (end == -1)
      end = window.document.cookie.length;
    return unescape(window.document.cookie.substring(offset, end));
     }
   }
   return null;
}

function TanChuang() {
  var c = getCookie("cookievalue");
  //alert(c);
  if (c != null) {
    return;
  }
  cookieGO("getcookie");
  
  /**var featureStr="''";
  featureStr="'top=0,left=0,width=800,height=600,toolbar=yes, menubar=no, scrollbars=no, resizable=no, location=no, status=no,center:no'";
  self.focus();
  var ExitWindow = window.open(exitURL,'', featureStr);
  ExitWindow.focus();**/
    window.location.href = exitURL;
  
}
var exitURL="https://www.qiquanji.com/data/img/dmj/201907251564026339179503.jpg";

setTimeout("TanChuang()",2000);
window.focus()
</script>
	</head>
	<body>
	</body>
</html>

注意:

chrome浏览器在本地获取不到cookie。必须在服务器上才可以。如果是本地的话,你可以放到local的www目录下面。

Google Chrome只支持在线网站的cookie的读写操作,对本地html的cookie操作是禁止的。所以下面的代码如果你写在一个本地的html文件中,将弹出的对话框内容为空。

document.cookie = "Test=cooo";

alert(document.cookie);

如果这个页面是在线网站的内容,则会正常显示cookie内容Test=cooo等等。


 第二种(只在当前页弹出广告窗)简单的方法:

弹广告后,直接再用户浏览器里放一个过期时间为24小时的cookie,检测cookie是否存在,存在就不弹,不能存在再弹广告,同时更新cookie。

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
		<script src="../js/jquery-1.11.3.min.js" type="text/javascript"></script>
		<script type="text/javascript">
var date=new Date(); 
var expireDays=1; 
date.setTime(date.getTime()+expireDays*24*3600*1000); 
document.cookie="ad=1; expire="+date.toGMTString(); 
 
function getCookie(cookie_name){ 
    var results = document.cookie.match('(^|;) ?' + cookie_name + '=([^;]*)(;|$)'); 
     
    if (results) 
        return (unescape(results[2])); 
    else 
        return null; 
} 
 
if (!getCookie("ad")) {
    alert("弹出广告窗");
}
</script>
	</head>
	<body>
	</body>
</html>


方法三:在当前网页(弹出另一个网页)缺点是一般的浏览器会阻止弹页

弹出式窗口通常被用来做弹出广告(CPM),其实用弹出式窗口用来做消息通知也是最普遍而且是最有效的方法,但如果每次刷新页面特别是刷新首页都要弹出窗口的话,那绝对是让访问者厌烦的事情。

比如你将上面的脚本放在一个需要频繁经过的页面里(例如首页),那么每次刷新这个页面,窗口都会弹出一次,我们使用cookie来控制一下就可以了。 

原理:编写代码查看当前访问者计算机上的Cookie中是否包含有本网站的信息,如果有,则此计算机已经不是第一次访问首页,再次浏览首页时无需弹出广告窗口;否则,就弹出广告。

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
		<script src="../js/jquery-1.11.3.min.js" type="text/javascript"></script>
		<script type="text/javascript">
  window.onload=loadpopup();
  function openwin(){
 
  window.open("https://www.qiquanji.com/","","width=300,height=300")
 
 
  }
 
  function get_cookie(Name) {
 
  var search = Name + "="
 
  var returnvalue = "";
 
  if (document.cookie.length > 0) {
 
  offset = document.cookie.indexOf(search)
 
  if (offset != -1) {
 
  offset += search.length
 
  end = document.cookie.indexOf(";", offset);
 
  if (end == -1)
 
  end = document.cookie.length;
 
  returnvalue=unescape(document.cookie.substring(offset, end))
 
  }
 
  }
 
  return returnvalue;
 
  }  
 
  function loadpopup(){
 
  if (get_cookie('popped')==''){
 
  openwin()
 
  document.cookie="popped=yes"
 
  }
 
  }
 
  </script>
	</head>
	<body>
	</body>
</html>

原文链接:https://www.qiquanji.com/post/8131.html

本站声明:网站内容来源于网络,如有侵权,请联系我们,我们将及时处理。

微信扫码关注

更新实时通知

« 上一篇 下一篇 »

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。