PDA

View Full Version : Cookie driven index page rotation



aj77
11-28-2008, 09:59 AM
Hey all,
I'm looking for some basic enough code, but obviously beyond me at this stage.

It seems it could be a javascript thing.

I need an index page to load a cookie with the user so that each time they load the site they are brought to a different index page. There are five index pages and must load in a particular sequence, ie not randomly.

If anyone has any examples of this working or any code it would be greatly appreciated!

Many thanks!

jscheuer1
11-28-2008, 11:05 AM
Before anyone goes writing code for this, we should first clarify what you mean by:


each time they load the site they are brought to a different index page. There are five index pages and must load in a particular sequence, ie not randomly

This almost sounds too specific a task for javascript. For example, if javascript, or simply javascript cookies are disabled in the user's browsers, your 'must' will not happen. This also sounds like something that you would like to carry over across browser sessions. What this means is that say someone visits the site, closes their browser, then opens the browser and visits again - you would want the next index page in sequence to load. If I'm right about that, if javascript driven, the user may have all javascript cookies set to expire on browser close, so it would not work for those folks.

Additionally, with javascript cookies, any given user may choose at any time to clear all javascript cookies from the browser with a few clicks or less.

So, if this is really all that important, a server side solution would need to be worked out.

If on the other hand, this is just something that you would like to have happen in most cases, javascript can do that. It would be a bit complicated and could have various approaches, but could still fairly easily be worked out. But, as I've implied, is not worth even thinking about in javascript if it 'must' happen.

aj77
12-01-2008, 03:42 PM
I think its more a matter that, it would be great to get it working like this, rather than a cast iron solution.

I think javasscript would be the simpler option and I think simplicity is the guiding factor here.

Would you happen to have any code set up to work like that?

Many thanks,

jscheuer1
12-01-2008, 05:15 PM
Here's a little demo. Each time the index page loads, it checks and sets a cookie to decide whether to remain on the index page or to switch to one of three other pages:

2264

Let me know what you think, it's very basic and should be easy to modify if needed/desired. The only code that really matters (the cookie routines are all basic stuff for working with cookies) is:


var numPages = 3
if(readCookie('whichPage') && readCookie('whichPage') !=0){
window.location.replace('index' + readCookie('whichPage') + '.htm');
createCookie('whichPage', (readCookie('whichPage') < numPages? readCookie('whichPage') - 0 + 1 : 0));
}
else
createCookie('whichPage', 1)

atom k
12-24-2008, 03:42 PM
That works perfectly.

How would I change it so, instead of rotating, it simply stops at the last page.

In my case I just have two pages, and would like users to see the first page only once (kind of like an intro on a DVD that you only see the first time you put in the disc, before you get to the menu.)

Thanks!

jscheuer1
12-24-2008, 04:02 PM
Say you have just index.htm and index1.htm, and index.htm is your 'splash page'. Change the code block from my previous post to:


if(readCookie('whichPage'))
window.location.replace('index1.htm');
else
createCookie('whichPage', 1);

But this isn't foolproof, as mentioned before in this thread, and it's not real SEO friendly either.