![]() |
Javascript to alternate between two sources
Anyone have a code handy that would allow you to alternate between two javascript sources with each page load? Right now I have:
<SCRIPT LANGUAGE="JavaScript" SRC="1.js"> </SCRIPT> And <SCRIPT LANGUAGE="JavaScript" SRC="2.js"> </SCRIPT> I'd like to only load one, and make it 50/50 between 1.js and 2.js |
you could save the last one loaded in a cookie and then keep alternating... if you don't need to it to be absolutely perfect an easier method would be to just use a random number, 1 or 2 :P
|
You will not get a solid 50/50 split unless you keep a count for each one. Random is usually pretty close but it could easily go 60/40 or worse, or better... it's called random for a reason.
|
If you want an even 50/50 split, your best implementation would most likely be accomplished server side, using PHP etc..
|
Quote:
|
Would this do it?
<script type="text/javascript"> <!-- var rand = Math.floor(Math.random()*2)+1; document.write('<script type="text/javascript" src="'+rand+'.js");</script>'); </script> |
If PHP is in your reach, try:
<SCRIPT LANGUAGE="JavaScript" SRC="<?=rand(1,2)?>.js"> If not: <script> var oHead = document.getElementsByTagName('HEAD').item(0); var oScript= document.createElement("script"); oScript.type = "text/javascript"; oScript.src= Math.floor ( Math.random ( ) * 2 + 1 )+".js"; oHead.appendChild( oScript); </script> |
Quote:
|
Thanks Quantum, I'll give it a shot. :)
|
Quote:
|
Quote:
Also, BestXXXPorn is correct. The sample size plays a huge role. If you have 100 hits, it could very easily go 60/40. |
Quote:
Previous flips do not influence this chance. Therefore, it WILL average out very close to 50/50 over a large sample set - that's why I said 'flip a coin 1,000,000 times' :winkwink: |
do true unique page load, every next time different script
|
Quote:
Not to mention if he wanted to add a 3rd option, the "averages" are much worse. |
All times are GMT -7. The time now is 12:07 AM. |
Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2025, vBulletin Solutions, Inc.
©2000-, AI Media Network Inc123