PDA

View Full Version : redirect URL based on number of page views


Ricky158
11-16-2002, 11:18 PM
i have this code so far:


<head>
<SCRIPT LANGUAGE="JavaScript">
<!-- Begin
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 = (argc > 2) ? argv[2] : null;
var path = (argc > 3) ? argv[3] : null;
var domain = (argc > 4) ? argv[4] : null;
var secure = (argc > 5) ? 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 DeleteCookie (name) {
var exp = new Date();
exp.setTime (exp.getTime() - 1);
var cval = GetCookie (name);
document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
}
var expDays = 30;
var exp = new Date();
exp.setTime(exp.getTime() + (expDays*24*60*60*1000));
function amt(){
var count = GetCookie('count')
if(count == null) {
SetCookie('count','1')
return 1
}
else {
var newcount = parseInt(count) + 1;
DeleteCookie('count')
SetCookie('count',newcount,exp)
return count
}
}
function getCookieVal(offset) {
var endstr = document.cookie.indexOf (";", offset);
if (endstr == -1)
endstr = document.cookie.length;
return unescape(document.cookie.substring(offset, endstr));
}
// End -->
</SCRIPT>
</head>
<body>
<SCRIPT LANGUAGE="JavaScript">
<!-- Begin
document.write("You have visited this page<font color=gold>" + amt() + "</font> times.")
// End -->
</SCRIPT>


i need to know (if possible, i believe it is) how to redirect someone to "URL2" if they have visited the page (using the code above as a base) let's say... 25 times. so if they were to visit for the 25th time, they would automatically be redirected to URL2. and after the 25th time at URL2, they would be redirected to URL1. it seems silly and unnecessary, but this would help me keep bandwidth to a minimum to redirect people, or at least i hope so.

(side note) URL2 is identical down to every picture, text size/color, and content, with URL1. so they would notice absolutely nothing different. both URLs have a series of pages in it. it's basically an entire web site duplicated.


also, since this is a tiny problem i'm having and it shouldnt require an entirely new thread just for itself, what is the script so that someone clicks a link and then it refreshes the page?

and one more question. is there a way to put a variable on URL1 so i could just do document.write on URL2 so i dont have to update both pages with the same changes?


thanks. an answer to any of my problems is appreciated.

glenngv
11-18-2002, 06:19 AM
<head>
<SCRIPT LANGUAGE="JavaScript">
<!-- Begin

//Cookie functions here

var numOfVisits = amt();
if (numOfVisits==24)
location.href="URL2";
else if (numOfVisits>24)
location.href="URL1";

// End -->
</SCRIPT>
</head>
<body>
<SCRIPT LANGUAGE="JavaScript">
<!-- Begin
document.write("You have visited this page<font color=gold>" + numOfVisits+ "</font> times.")


I compared it to 24 since the amt() function returns the number of PREVIOUS visits excluding the current one.
So on the 25th visit, the function would return 24 which is the previous number of visits.

If you want to include the current visit in the total number of visits, then you should return newcount instead of count in the amt() function:

function amt(){
...
else {
...
return newcount;
}
}

consequently, change 24 to 25 in the code above.

Disabled1
11-14-2006, 10:21 PM
could someone point me to where the code in the second post would replace the script in the original post please?
Thank You

Im learning

Disabled1
11-15-2006, 04:32 PM
Can anyone help me please? I have tried and tried to make it work as it is exactly what i have needed. Ive spent almost a week trying myself but I not been able to do it. I have no one else to ask.

Thank You for your time.
Val

rubenbuhr
11-17-2006, 12:29 AM
I'm not going to try and pick through the codes above. The original thread starter has his cookie script, I have mine.

Here is a complete example if you wish to look it over (attached)

It's only the last four lines in the script that counts and redirects after 25 visits to the page. The cookie expires in 30 days.

Disabled1
11-17-2006, 12:50 AM
Thank You!

I will look it over and see if I can figure it out to use. If that is OK with you?
I sure appreciate the time you took to read and reply :).

I am excited to check it out.

Thank You Again,

Valorie