GoFuckYourself.com - Adult Webmaster Forum

GoFuckYourself.com - Adult Webmaster Forum (https://gfy.com/index.php)
-   Fucking Around & Business Discussion (https://gfy.com/forumdisplay.php?f=26)
-   -   Javascript to alternate between two sources (https://gfy.com/showthread.php?t=972964)

dready 06-11-2010 11:43 AM

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

BestXXXPorn 06-11-2010 11:49 AM

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

ProG 06-11-2010 11:53 AM

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.

AIbenjamink 06-11-2010 12:08 PM

If you want an even 50/50 split, your best implementation would most likely be accomplished server side, using PHP etc..

quantum-x 06-11-2010 12:11 PM

Quote:

Originally Posted by ProG (Post 17238509)
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.

Bullshit. Go flip a coin 1,000,000 times and tell me if it goes out to 60/40.

dready 06-11-2010 12:14 PM

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>

quantum-x 06-11-2010 12:17 PM

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>

quantum-x 06-11-2010 12:18 PM

Quote:

Originally Posted by dready (Post 17238571)
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>

Also good :)

dready 06-11-2010 12:19 PM

Thanks Quantum, I'll give it a shot. :)

BestXXXPorn 06-11-2010 12:24 PM

Quote:

Originally Posted by quantum-x (Post 17238563)
Bullshit. Go flip a coin 1,000,000 times and tell me if it goes out to 60/40.

It IS possible... unlikely but still possible... It also has a LOT to do with sample size :P

ProG 06-11-2010 12:54 PM

Quote:

Originally Posted by quantum-x (Post 17238563)
Bullshit. Go flip a coin 1,000,000 times and tell me if it goes out to 60/40.

Not sure what you are saying. I know you are smart enough to know that random will never be equal. The FACT is, if you want it to be 50/50 exactly, random isn't going to work.

Also, BestXXXPorn is correct. The sample size plays a huge role. If you have 100 hits, it could very easily go 60/40.

quantum-x 06-11-2010 01:28 PM

Quote:

Originally Posted by ProG (Post 17238675)
Not sure what you are saying. I know you are smart enough to know that random will never be equal. The FACT is, if you want it to be 50/50 exactly, random isn't going to work.

Also, BestXXXPorn is correct. The sample size plays a huge role. If you have 100 hits, it could very easily go 60/40.

It's really simple: When a coin is flipped, it is a 50% chance of being heads or tails.
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:

seeandsee 06-11-2010 01:37 PM

do true unique page load, every next time different script

ProG 06-11-2010 01:44 PM

Quote:

Originally Posted by quantum-x (Post 17238767)
It's really simple: When a coin is flipped, it is a 50% chance of being heads or tails.
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:

The OP didn't ask for an average, he asked for a 50/50 split, which you can't guarantee with random. Just because you have a 50% chance of getting heads or tales does not mean you get 50% heads and 50% tales. If you made a machine to flip coins exactly the same every time (removing the random factor) you would always get the same result not 50/50.

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