...

View Full Version : different page depending on how many times loaded the page



squit
07-04-2003, 11:54 AM
Hi all there. I'm stuck with making a js. I found some cool scripts on this site.

these:
-alert message that says how many times you've been on the site(http://javascriptkit.com/script/script2/visitorinfo.shtml)
-and another version of this, but thisone says it in the html itself. (http://javascriptkit.com/script/cut13.shtml)

now i wanted to make a js that does this:
-when you enter the page for the 1th time, it will say:"hello, you're new here. on the left is a menu etc etc..."
-when you enter the site for the second time it will say: "hello, welcome back. Do you remember everything that is on this site? here a quick guide etc etc"

I have this till now:

<BODY>


<SCRIPT LANGUAGE="JavaScript">

function getCookieVal (offset) {
var endstr = document.cookie.indexOf (";", offset);
if (endstr == -1)
endstr = document.cookie.length;
return unescape(document.cookie.substring(offset, endstr));
}
function GetCookie (name) {
var arg = name + "=";
var alen = arg.length;
var clen = document.cookie.length;
var i = 0;
while (i < clen) {
var j = i + alen;
if (document.cookie.substring(i, j) == arg)
return getCookieVal (j);
i = document.cookie.indexOf(" ", i) + 1;
if (i == 0)
break;
}
return null;
}
function SetCookie (name, value) {
var argv = SetCookie.arguments;
var argc = SetCookie.arguments.length;
var expires = (2 < argc) ? argv[2] : null;
var path = (3 < argc) ? argv[3] : null;
var domain = (4 < argc) ? argv[4] : null;
var secure = (5 < argc) ? argv[5] : false;
document.cookie = name + "=" + escape (value) +
((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
((path == null) ? "" : ("; path=" + path)) +
((domain == null) ? "" : ("; domain=" + domain)) +
((secure == true) ? "; secure" : "");
}
function DisplayInfo() {
var expdate = new Date();
var visit;
expdate.setTime(expdate.getTime() + (24 * 60 * 60 * 1000 * 365));
if(!(visit = GetCookie("visit")))
visit = 0;
visit++;
SetCookie("visit", visit, expdate, "/", null, false);
var message;

if(visit == 1)
document.write("1");
if(visit>= 2)
document.write("2")


}
function ResetCounts() {
var expdate = new Date();
expdate.setTime(expdate.getTime() + (24 * 60 * 60 * 1000 * 365));
visit = 0;
SetCookie("visit", visit, expdate , "/", null, false);
history.go(0);
}

window.onload=DisplayInfo
</Script>


the part that is written in blue says that when you enter the site for the first time, it will say 1,
when you enter for the 2nd or more time, it will say 2.

now the problem is, is that when you enter the site for the first time, you will see a 1. but thats it. when I checked out the source, all i saw was 1 (same for 2nd time, but then I saw a 2).

Wich means that the body tags above for example don't work. So I figured that, when I post an entire html in the document.write tag it will display a html. BUT also that doesn't work. because body tags for example contain these characters "

this character also lets the js think that the document.write script ends.........

so what must I do to make this work!?!?!?!? I don't know it anymore..........

Philip M
07-04-2003, 06:53 PM
if(visit == 1)
document.write("1");
if(visit>= 2)
document.write("2")

This does what you see - it writes the literal character 1 or 2 depending on the value of visit.

Try this:-

if (visit != null) {
switch (visit) {
case '1' : url = 'page1.html'; // change these to suit!
break;
case '2' : url = 'page2.html';
break;

.........and so on if desired

}
window.location.href = url;
}

squit
07-06-2003, 08:35 PM
so when I change it it should be like this:



<BODY>


<SCRIPT LANGUAGE="JavaScript">

function getCookieVal (offset) {
var endstr = document.cookie.indexOf (";", offset);
if (endstr == -1)
endstr = document.cookie.length;
return unescape(document.cookie.substring(offset, endstr));
}
function GetCookie (name) {
var arg = name + "=";
var alen = arg.length;
var clen = document.cookie.length;
var i = 0;
while (i < clen) {
var j = i + alen;
if (document.cookie.substring(i, j) == arg)
return getCookieVal (j);
i = document.cookie.indexOf(" ", i) + 1;
if (i == 0)
break;
}
return null;
}
function SetCookie (name, value) {
var argv = SetCookie.arguments;
var argc = SetCookie.arguments.length;
var expires = (2 < argc) ? argv[2] : null;
var path = (3 < argc) ? argv[3] : null;
var domain = (4 < argc) ? argv[4] : null;
var secure = (5 < argc) ? argv[5] : false;
document.cookie = name + "=" + escape (value) +
((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
((path == null) ? "" : ("; path=" + path)) +
((domain == null) ? "" : ("; domain=" + domain)) +
((secure == true) ? "; secure" : "");
}
function DisplayInfo() {
var expdate = new Date();
var visit;
expdate.setTime(expdate.getTime() + (24 * 60 * 60 * 1000 * 365));
if(!(visit = GetCookie("visit")))
visit = 0;
visit++;
SetCookie("visit", visit, expdate, "/", null, false);
var message;

if (visit != null) {
switch (visit) {
case '1' : url = 'home.html'; // change these to suit!
break;
case '2' : url = 'home2.html';
break;


}
function ResetCounts() {
var expdate = new Date();
expdate.setTime(expdate.getTime() + (24 * 60 * 60 * 1000 * 365));
visit = 0;
SetCookie("visit", visit, expdate , "/", null, false);
history.go(0);
}

window.onload=DisplayInfo
</Script>




if it must be this, then you're wrong. because it doesn't work...:(

Philip M
07-07-2003, 06:43 AM
Well, no, it doesn't.

But it might if you followed what I suggested to you.

if (visit != null) {
switch (visit) {
case '1' : url = 'page1.html'; // change these to suit!
break;
case '2' : url = 'page2.html';
break;
}
window.location.href = url;
}


This assumes that the variable 'visit' has the values of 1, 2 etc.

squit
07-07-2003, 09:42 AM
DOH! Shame on me.......So I changed it, and it still doesn't work....;)

squit
07-07-2003, 11:58 AM
I know that there's a way to read from an external .js file. that goes like this

<script src="example.js"></script>
in the .js file you put something like this for example:

document.write("hello")
when you open the html file, you will see 'hello'

isn't there a way to combine this script with the one i am making know?? So you get something like this:

if(visit == 1)
read from external js file here
if(visit>= 2)
read from 2nd external js file here

how should the tag to read from the js file be??



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum