Show jQuery popup once per visitor
How do I get this script to only run if the person is a new visitor to the site? It runs every time the page is requested which becomes very annoying.
//0 means disabled; 1 means enabled;
var popupStatus = 0;
function loadPopup(){
//loads popup only if it is disabled
if(popupStatus==0){
$("#backgroundPopup").css({
"opacity": "0.7"
});
$("#backgroundPopup").fadeIn("slow");
$("#popupContact").fadeIn("slow");
popupStatus = 1;
}
}
//disabling popup with jQuery magic!
function disablePopup(){
//disables popup only if it is enabled
if(popupStatus==1){
$("#backgroundPopup").fadeOut("slow");
$("#popupContact").fadeOut("slow");
popupStatus = 0;
}
}
//centering pop开发者_如何转开发up
function centerPopup(){
//request data for centering
var windowWidth = document.documentElement.clientWidth;
var windowHeight = document.documentElement.clientHeight;
var popupHeight = $("#popupContact").height();
var popupWidth = $("#popupContact").width();
//centering
$("#popupContact").css({
"position": "absolute",
"top": windowHeight/2-popupHeight/2,
"left": windowWidth/2-popupWidth/2
});
//only need force for IE6
$("#backgroundPopup").css({
"height": windowHeight
});
}
$(document).ready(function(){
//LOADING POPUP
//Click the button event!
//centering with css
centerPopup();
//load popup
loadPopup();
//CLOSING POPUP
//Click the x event!
$("#popupContactClose").click(function(){
disablePopup();
});
//Click out event!
$("#backgroundPopup").click(function(){
disablePopup();
});
//Press Escape event!
$(document).keypress(function(e){
if(e.keyCode==27 && popupStatus==1){
disablePopup();
}
});
});
set a cookie set to expire at end of session with a value of 1 or true or whatever. Then look for cookie. If it exists, don't show it.
As already said you need to create a cookie the first time a user sees the page and then check that the cookie exists on other visits.
The JavaScript to create Cookies isn't as easy as it could be because you need to set a string using a precise format. It's easier to use these helper functions that can be found on Quirksmode:
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;
}
Then in your code all you need to do is:
if(readCookie("popupShown") == null) {
// Create cookie for 30 days
createCookie("popupShown", 1, 30);
yourPopupFunction();
}
Use cookies if you want a purely client-side method. Set a flag saying the user has already seen your intro or whatever it is.
In the case of a site with some kind of authentication this could be handled by the server.
God bless you!
精彩评论