PDA

View Full Version : detect new visitor



Icy Penguin
March 7th, 2007, 12:00 AM
Hello everyone,

Just wondering how I should go about changing some a paragraph of text from 'hello new user' to 'hello n00b' if the user hasnt been to my site before.

so far I've been fooling around with this JS:



function bakeCookie(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 salivateAfterCookie(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;
}

function eatCookie(name) {
bakeCookie(name,"",-1);
}

window.onload = function() {
blah = document.getElementById('begin');
if(salivateAfterCookie('visitor') == 'old') {
document.write('hello again')
} else {
document.write('hello n00b')
}
bakeCookie('visitor', 'old', 999)
}



but, when i visit the page, only the 'hello again'/'hello n00b' text displays, and the rest of the site doesnt.
also, how can i get it to write into one specific div?

or shoudl i do all this in .php?


thanks

Jeff Wheeler
March 7th, 2007, 12:05 AM
Use DOM functions.

Javascript DOM

borrob
March 7th, 2007, 02:07 AM
window.onload = function() {
blah = document.getElementById('begin');
if(salivateAfterCookie('visitor') == 'old') {
document.write('hello again')
} else {
document.write('hello n00b')
}
bakeCookie('visitor', 'old', 999)
}

change this:

window.onload = function() {
var obj = document.getElementById( "some_div" );

blah = document.getElementById('begin');
if(salivateAfterCookie('visitor') == 'old') {
obj.innerHTML = 'hello again';
} else {
obj.innerHTML = 'hello n00b';
}
bakeCookie('visitor', 'old', 999)
}


make some div in your html page:

<div id="some_div"></div>

have fun.

Icy Penguin
March 7th, 2007, 07:57 PM
ah. works great. thanks :)