简体   繁体   中英

Show hidden div on third page load

I have a function that shows a hidden div special offer to visitors on page load and then sets a cookie so it only shows on the first time the user loads the page. I would like the ability to set a number of visits required before the div is shown so i can only show the hidden div on say the 3rd page load. But I have not been able to figure out how to make this happen. Can anyone point me in the right direction?

function CreatePopup(url, height, duration, description, lifetime) {
// Exit if the current browser has already received the popup, or 
// the browser is not supported (IE6).
if (HasAlreadyReceivedPopup(description) || IsUnsupportedUserAgent())
    return;

$.get(url, function(data) { 
    /*var popup = $("<div>" + data + "</div>")
        .attr({ "id": "sliding_popup" })
        .css({"bottom": -1 * height})
        .height(height)
        .hide()
        .appendTo("body");*/
        popup = $("<div>" + data + "</div>")
        .attr({ "id": "sliding_popup" })
        .hide()
        .appendTo("body");



    ShowPopup(description, lifetime, popup, duration); 
    });
}

function ShowPopup(description, lifetime, popup, duration) 
{ 

popup.show().animate( { top: 100 }, duration);
ReceivedPopup(description, lifetime);
}

function HasAlreadyReceivedPopup(description) { 
return document.cookie.indexOf(description) > -1; 
}

function ReceivedPopup(description, lifetime) { 
var date = new Date(); 
date.setDate(date.getDate() + lifetime); 
document.cookie = description + "=true;expires=" + date.toUTCString() + ";path=/";  
}

function IsUnsupportedUserAgent() { 
return (!window.XMLHttpRequest); 
}

function DestroyPopup(duration) {
$("#sliding_popup").animate({ top: $("#sliding_popup").height() * -1 }, duration,     function () { $("#sliding_popup").remove(); })
}

function SecondVisit(description)
{ 
 if (HasAlreadyReceivedPopup(description)) {
  return true;
 }
  else
 {
 return false;
 }

}

The function is called in an include like so:

The function is called like so

$(document).ready(function () { 
// This check is used by the demo to allow you to remove the cookie. Do not use in production code.
if (HasAlreadyReceivedPopup("promo"))
$("#note").show().click(function () { 
document.cookie = name + "promo=;path=/;expires=Thu, 01-Jan-1970 00:00:01 GMT"; location.reload(true); 
});

CreatePopup("page.html", 300, 1500, "promo", 5); 
});

Create a cookie with some name related to your special offer dialog and store IP address of a user and amount of times he visited page. So, if user doesn't have your "special" cookie, you create it and assign amount of visits to 1. On all further visits just increment this amount and once it is 3 or whatever amount you need show your dialog.

Just make the cookie a counter and count the n times you want your offer to pop.

If the n times are not matched, each visit to the page would increase the visitor's counter until the nth time. You check this simply with an if(). If it is set then: make the cookie reset back to 0 times (if you want it to show again in nth time) and display the popup.

Use $.cookie since you are using jquery since it's better handled javascript.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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