/    Sign up×
Community /Pin to ProfileBookmark

Please help with this code

This code drops in a div. How can I edit this code to only execute on the first page a visitor views? In other words, if a visitor clicks on another link in my site I do not want this code to trigger again.

var ie=document.all
var dom=document.getElementById
var ns4=document.layers
var bouncelimit=32
var direction=”up”
function initbox(){
if (!dom&&!ie&&!ns4)
return
crossobj=(dom)?document.getElementById(“dropin”).style : ie? document.all.dropin : document.dropin
scroll_top=(ie)? document.body.scrollTop : window.pageYOffset
crossobj.top=scroll_top-250
crossobj.visibility=(dom||ie)? “visible” : “show”
dropstart=setInterval(“dropin()”,600)
}
function dropin(){
scroll_top=(ie)? document.body.scrollTop : window.pageYOffset
if (parseInt(crossobj.top)<100+scroll_top)
crossobj.top=parseInt(crossobj.top)+40
else{
clearInterval(dropstart)
bouncestart=setInterval(“bouncein()”,50)
}
}
function bouncein(){
crossobj.top=parseInt(crossobj.top)-bouncelimit
if (bouncelimit<0)
bouncelimit+=8
bouncelimit=bouncelimit*-1
if (bouncelimit==0){
clearInterval(bouncestart)
}
}
function dismissbox(){
if (window.bouncestart) clearInterval(bouncestart)
crossobj.visibility=”hidden”
}
window.onload=initbox

function getexpirydate( nodays){
var UTCstring;
Today = new Date();
nomilli=Date.parse(Today);
Today.setTime(nomilli+nodays*24*60*60*1000);
UTCstring = Today.toUTCString();
return UTCstring;
}
function getcookie(cookiename) {
var cookiestring=””+document.cookie;
var index1=cookiestring.indexOf(cookiename);
if (index1==-1 || cookiename==””) return “”;
var index2=cookiestring.indexOf(‘;’,index1);
if (index2==-1) index2=cookiestring.length;
return unescape(cookiestring.substring(index1+cookiename.length+1,index2));
}
function setcookie(name,value,duration){
cookiestring=name+”=”+escape(value)+”;EXPIRES=”+getexpirydate(duration);
document.cookie=cookiestring;
if(!getcookie(name)){
return false;
}
else{
return true;
}
}
function date1(){
now = new Date();
then = new Date(” Jan 01 1970 00:00:00″);
seconds=now-then/1000;
month=1+now.getMonth();
day=now.getDate();
year=now.getFullYear();
document.write( day+”-“+month+”-“+year+””);
}

to post a comment
JavaScript

7 Comments(s)

Copy linkTweet thisAlerts:
@konithomimoFeb 11.2007 — The best way is to use server side sessions. The nly client-side way is as you have above with the cookie. First check to see if the cookie exists, if so then don't drop in. If not then drop in. Set the cookie to expire whenever you want. Of course, some people dont have cookies enabled, so server-side sessions are the way to go.
Copy linkTweet thisAlerts:
@jseamlessauthorFeb 12.2007 — Someone want to take a stab at this? I do not know any JavaScript programming.
Copy linkTweet thisAlerts:
@konithomimoFeb 12.2007 — It all depends on what you mean by "first page". a "first page" can refer to an index page, the first page a user sees when visiting your site for the first time, or the first page they view each time that they come to your site. If it is the last one then you will have a problem unless you are using frames/server-side includes and only having one main page, which has the content changing, or if you use some set number of times before the cookie expires, but if someone is on your page for a long time then they will see it multiples times. Of course, you can set the cookie to expire each day and require it to be reset each day. But like I already said, some people don't enebale cookies, so your script will not work for everyone.
Copy linkTweet thisAlerts:
@jseamlessauthorFeb 12.2007 — The first page a user sees when visiting your site for the first time.
Copy linkTweet thisAlerts:
@konithomimoFeb 12.2007 — Just set an expiration date of your cookie to be a long time off, and then check to see fi the cokkie exists. if it does then dont popin the div. That means that you dont need:

function getexpirydate( nodays){
var UTCstring;
Today = new Date();
nomilli=Date.parse(Today);
Today.setTime(nomilli+nodays*24*60*60*1000);
UTCstring = Today.toUTCString();
return UTCstring;
}




or

function date1(){
now = new Date();
then = new Date(" Jan 01 1970 00:00:00");
seconds=now-then/1000;
month=1+now.getMonth();
day=now.getDate();
year=now.getFullYear();
document.write( day+"-"+month+"-"+year+"");
}


Then change this:

function setcookie(name,value,duration){

cookiestring=name+"="+escape(value)+";EXPIRES="+getexpirydate(duration);

to this:

function setcookie(name,value){

var expiration_date = new Date("January 1, 3000");

expiration_date = expiration_date.toGMTString();

cookiestring=name+"="+escape(value)+";EXPIRES="+getexpirydate(duration);

and then your getcookie() function should only check to see if the cookiename is found. If not then it drops in the box and creates the cookie.

Lastly, call getcookie() onload instead of initbox
Copy linkTweet thisAlerts:
@konithomimoFeb 12.2007 — Doing all of that will give you this:
var ie=document.all
var dom=document.getElementById
var ns4=document.layers
var bouncelimit=32
var direction="up"
var cookiename = 'mycookie';
function initbox(){
if (!dom&amp;&amp;!ie&amp;&amp;!ns4)
return
crossobj=(dom)?document.getElementById("dropin").style : ie? document.all.dropin : document.dropin
scroll_top=(ie)? document.body.scrollTop : window.pageYOffset
crossobj.top=scroll_top-250
crossobj.visibility=(dom||ie)? "visible" : "show"
dropstart=setInterval("dropin()",600)
}
function dropin(){
scroll_top=(ie)? document.body.scrollTop : window.pageYOffset
if (parseInt(crossobj.top)&lt;100+scroll_top)
crossobj.top=parseInt(crossobj.top)+40
else{
clearInterval(dropstart)
bouncestart=setInterval("bouncein()",50)
}
}
function bouncein(){
crossobj.top=parseInt(crossobj.top)-bouncelimit
if (bouncelimit&lt;0)
bouncelimit+=8
bouncelimit=bouncelimit*-1
if (bouncelimit==0){
clearInterval(bouncestart)
}
}
function dismissbox(){
if (window.bouncestart) clearInterval(bouncestart)
crossobj.visibility="hidden";
}

function getcookie() {
var cookiestring=""+document.cookie;
var index1=cookiestring.indexOf(cookiename);
if (index1==-1){
initbox;
setcookie();
}
return true;
}

function setcookie(){
var expiration_date = new Date("January 1, 3000");
expiration_date = expiration_date.toGMTString();
cookiestring="name="+cookiename+";EXPIRES="+expiration_date;
document.cookie=cookiestring;
if(!getcookie()){
alert('You have cookies turned off.');
return false;
}
else
return true;
}

window.onload=getcookie;


The box will only "drop in" when the user first visits the site, deletes the cookie, or every time if the user doesnt have cookies enabled.
Copy linkTweet thisAlerts:
@jseamlessauthorFeb 16.2007 — I appreciate everyone help. The code provided by konithomimo does not seem to be working.
×

Success!

Help @jseamless spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...