首次加载时只加载一次广告(div) [英] load an ad (div) just once on first load

查看:72
本文介绍了首次加载时只加载一次广告(div)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何才能让广告加载或只在第一次加载页面时显示,但每次刷新页面时都会隐藏它?

I wanted to know how can I have a div that has an ad to load or become visible ONLY the first time you load the page but hide it every time the page is refreshed?

我只有用Jquery加载div的代码,但在刷新页面后不知道如何隐藏它:

I only have the code to load the div with Jquery but don't know how to hide it after refreshing the page:

$(document).ready(function() {
  $(".referralProgram").fadeIn("slow");
});


推荐答案

使用cookie:

$(document).ready(function() {
    if (!readCookie("adSeen")) {
        $(".referralProgram").fadeIn("slow");
        createCookie("adSeen", "1", 1000);
    }
});

function createCookie(name, value, days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        var expires = "; expires=" + date.toGMTString();
    } else var expires = "";
    document.cookie = name + "=" + value + expires + "; path=/";
}

function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') c = c.substring(1, c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
    }
    return null;
}

来自 quirksmode.org

编辑:既然如此如果禁用cookie,许多人正在讨论如何处理这个问题,应该指出服务器会话实现依赖于cookie或URL中的会话标识符。充其量只要会话标识符位于网址中,您就只能阻止为同一用户显示广告。返回主页sans会话ID将重新显示广告。此外,如果用户共享URL,则粗心的实现(甚至一些仔细的实现)可能会导致其他用户误报。 localStorage 解决方案无法在大多数(如果不是全部)浏览器中禁用Cookie。

Since so many are discussing how to deal with this if cookies are disabled, it should be pointed out that server session implementations rely on either a cookie, or a session identifier in the url. At best, you could only prevent displaying the ad for the same user as long as the session identifier is in the url. Returning to the home page sans session id would re-display the ad. Additionally, a careless implementation (and even some careful implementations) could result in false positives for other users if a user shares a url. localStorage solutions won't work with cookies disabled in most, if not all, browsers.

这篇关于首次加载时只加载一次广告(div)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆