Copy the following code to your computer for testing:
Call the first file: test.htm
Drop this code into that file:
Call the second file: css.random.js
Drop this code into that file:
Call the third file: style1.css
Drop this code into that file:
Call the fourth file: style2.css
Drop this code into that file:
Call the fifth file: style3.css
Drop this code into that file:
Those are your test files. I kept the code real simple so that you can observe what happens each time the page loads up. Each time the page loads, the JavaScript generates a random number of 1, 2, or 3 which corresponds to the style sheet file name with the number as part of the file name.
Reload the test.htm page several times to see it cycle through the style sheets.
I just wrote this off the top of my head so excuse its shabbyness.. but you could simply have X style sheets and select them randomly..
This should work [untested]
the above script will select between 4 stylesheets, to get more or less swap the number.. call each style sheet "stylesheetX.css" where X is the number..
hope that makes sence, so simple but it should work
The only thing with the second JavaScript code is that it will allow for five numbers 0-4 because some numbers will round down to zero. So what appears to be 4 numbers is actually 5 numbers and so forth. You'll need to ensure that the number zero is included for your style sheets for the 2nd code.
The first JavaScript routine is designed to ensure that numbers picked are 1, 2, or 3 and so forth (depending on how many style sheets you want). Both are nearly equally simple really, but neither do exactly the same thing.
if (window.screen){
var ranNum= Math.round(Math.random()*4+1);
document.write("<link rel='stylesheet' href='pathto/stylesheet"+ranNum+".css' type='text/css'>";
}
You still have 5 style sheets even though the number looks like 4. The only way to ensure 1-4 is to use the first method. Can't get around the If-Then statement.
This will only ever produce numbers between 1 and 4
Explanation
math.random() creates a numer BETWEEN 0 and 1
so the highest valu is 0.99
0.99 * 4 = 3.96
3.96 + 1 = 4.96
math.floor, rounds down.. so turns the number into 4
math.random creates a figure between 0 and 1 with 15 decimal places..
so the highest value possible is
0.999999999999999
0.999999999999999*4 = 3.999999999999996
3.999999999999996+1 = 4.999999999999996
Math.floor(4.999999999999996) = 4
fluidblogger, it would seem you have something there (provided something else isn't happening in addition when random numbers are generated and multiplied). These hypothetical screwy programming rules can catch us off-guard. I attempted to cover every contingency in my code, but you probably have something there.
This reminds me of one of our clients who took elaborate pains to sniff out the type of browser plugins the visitor used and delivered tailor-made pages. Worked very well for most of the visitors but choked when no parameter was passed on to the PHP script. Robots and browsers which had Javascript turned off were fed with an almost blank page.
"Variables won't and constants aren't "
Some assumptions we make don't always correspond with realities.
Prowler, I can't imagine taking that kind of time to program the page to work on every browser. It's one thing to code up JavaScript to work well, but quite another to code up a page to work on every browser. For me, I'm quite happy if my JS works on Mozilla, MSIE, and perhaps Opera. The rest of the browsers are bonus if they execute the JS properly. If the rest don't, I do not lose sleep.
You are not permitted to post messages in this forum or topic, because of one or more of the following reasons:
You have not yet logged in, or registered properly as a member
You are a member, but no longer have posting rights.
This is a private forum, for which you do not have permissions.
If you are a recent member, it's possible that you simply have not yet confirmed your account. Please
check your email for a message entitled 'JimWorld Forums: Confirm Your Account' and follow the instructions
contained within.
If you are still experiencing problem, please read the
Login Assistance
Article for some advice on what may be causing your login not to work properly.