View Full Version : Style Sheet Switcher (v1.1) - random style on every new site visit

07-03-2008, 11:28 AM
1) Script Title: Style Sheet Switcher (v1.1)

2) Script URL (on DD): http://www.dynamicdrive.com/dynamicindex9/stylesheetswitcher.htm

3) Describe problem: This is the best styleswitching script I have come across and does almost everything you need but I was wandering, would it be possible to update the script to select a random css file on every new visit to the site?
What I am thinking of is very similar to the new browser session functionality which it currently has but what I was hoping was if the user browses away from the site and then back again (without closing the browser window) a new random css is called.
I'm not even sure if this is possible with javascript but would love it to be. :)

07-03-2008, 12:27 PM
Unless I read the script wrong, it looks like it can already do that. Here:

//Style Sheet Switcher version 1.1 Oct 10th, 2006
//Author: Dynamic Drive: http://www.dynamicdrive.com
//Usage terms: http://www.dynamicdrive.com/notice.htm

var manual_or_random="manual" //"manual" or "random"
var randomsetting="3 days" //"eachtime", "sessiononly", or "x days (replace x with desired integer)". Only applicable if mode is random.

//////No need to edit beyond here//////////////

function getCookie(Name) {
var re=new RegExp(Na . . .

Set it like:

var manual_or_random="random" //"manual" or "random"
var randomsetting="eachtime" //"eachtime", "sessiononly", or "x days (replace x with desired integer)". Only applicable if mode is random.

07-03-2008, 10:36 PM
No, I wish it did but I've looked into all those functions.
All 'eachtime' does is change the style sheet every time a page is entered so the stylesheet is different everytime you click an internal link on the site.

What I need it to do is stay the same for the whole visit (ie as long as the user is browsing around the site), then if they navigate away from the site and back again it changes.

I'm currently using 'sessiononly' which is nearly what I want but requires you to close the browser and then re-open before the style sheet changes.

07-04-2008, 07:43 AM
I don't think there is any reliable way to get the type of information you would need to do that.

In today's modern browsers it's tricky to even know where someone is in relation to your site because they may have more than one tab of it open, even with older non-tabbed browsers, they may have more than one window of it open. But basically what you are asking for is a way of knowing if the user has come to the page from within or from without of the site, that can be told sometimes via the document.referrer property, which will (if a link was used to get to your page) usually contain the URL of the page the user came from. But if someone just pastes the address in, or uses a bookmark to get to your page, or uses the back and forward buttons to navigate, that property will be empty, and there will be no way of knowing if they came from within or without.

Another idea I had was that we could have all links that leave your site delete the cookie. That would require a script that would initialize the links on every page, similar to an 'open off site links in a new window' script, only instead of opening a new window, it would delete the cookie. But users could open their own new window when they hit one of those links, so not really be leaving your site, or you may already be opening some or all off site links in a new window. And they could always leave your site without clicking on any link on it by using bookmarks or what have you, as mentioned before.

Before I started writing all of this, I thought, "I don't think there is any way to really know that". The more I think about it, the more I think I was right in the first place. I hope my above ruminations help you to see why though.

Then again, I may still be missing the point, as I obviously did at first with this. If you (or anyone really) can think of some measurable by javascript event that would demark when you want to change the style and when not, then we could go with that.

07-04-2008, 09:22 AM
Thanks again jscheuer1. It's always good to discuss these things and you raised some very valid and interesting points which have opened my eyes to the potential enormity of what I'm asking.

I'm a relative beginner with Javascript but am starting a bit of a frontend trial by fire and I think the sessiononly option does cover what I need for a job I'm doing at the moment.

This is one of those things that I'm sure can be done in, like you said, a measurable way. If I have a eureka moment I'll post it up!

07-04-2008, 10:05 AM
If you have PHP or another server side language available to you on your host, I believe there is or can be made to be a session variable that can track when a user is on the site, but I believe all of your pages would have to be server pages, and that even then, due to the complex nature of the way folks surf (or perhaps only if the tracking code were not well thought out), there could be slip ups, but much less likely than with all of the methods I was thinking about before.

However, I'm not 100% sure if this can be really effective without requiring a login. Anonymous users could be tracked by their IP address, but obviously others could have the same IP. I'm not real up on server side stuff though, so there may be a better way to track anons.

07-07-2008, 04:09 PM
You are right there. Php does have the capability of making a session variable available throughout the site. I use php on a day to day basis and I think it could be used to interact with this javascript as a kind of widget on every page.
I was hoping to do it with javascript alone thus making it much more robust and transferrable to any other project seamlessly.
Thanks for your attention to this topic though.