Log in

View Full Version : Code Randomizer



Jessic4
11-01-2012, 02:08 AM
Hi,

I was hoping someone might be able to help me, I wasn't sure whether to post on php or javascript.

I've created a toolbar at the bottom of my side that I'd like to use to rotate my Follow, Fan and Subscribe buttons ramdomly. I've seen a few javascripts out there that do this for quotes and images, but when using the arrays of javascript it creates a lot of confusion in having quotes and single quotes that are within the code, with an additional set of array quotes. What script would you recommend?

I want to randomize these 3 codes:


<div class="fb-like" data-href="https://facebook.com/***" data-send="false" data-width="320" data-show-faces="false" data-colorscheme="dark" data-font="trebuchet ms"></div>

<form id="mc-embedded-subscribe-form" name="mc-embedded-subscribe-form" method="post" class="validate" target="_blank"
action="http://lonlf.us5.list-manage1.com/subscribe/post?u=8200626871fa13">
<input style="margin-top: 36px; margin-left: 9px; margin-right: 9px;
width:140px;" name="EMAIL" id="mce-EMAIL" value="Enter your email..." type="email" class="required email" onfocus="if(this.value==this.defaultValue)this.value='';" onblur="if(this.value=='')this.value=this.defaultValue;">
<input type="submit" value="Subscribe" name="subscribe" id="mc-embedded-subscribe" class="button" style="vertical-align:bottom;">
</form>

<a href="https://twitter.com/Lon" class="twitter-follow-button" data-show-count="false" data-show-screen-name="false">Follow @Lon</a>
<script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src="//platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");</script>

bernie1227
11-01-2012, 06:38 AM
there's a whole thread on a script similar here:
http://www.dynamicdrive.com/forums/showthread.php?71494-random-and-rotating-tesimonials
you can stop any problems you have with multiple quote marks by simply escaping all of them before pasting them in.

Beverleyh
11-01-2012, 12:03 PM
Maybe you could save each code snippet into its own txt file and save them as 1.txt, 2.txt and 3.txt, in the same folder.

Then you could use php to randomise the numbers in an include;


<?php
$snippet = array('1', '2', '3');
shuffle($snippet);
include('path/to/snippets/'.$snippet.'.txt');
?>


Same method could be used for randomising images too - just change 'txt' in the code above to 'jpg' or whatever.

vwphillips
11-01-2012, 01:47 PM
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
<title></title>
<style type="text/css">
/*<![CDATA[*/
#tst {
position:absolute;left:100px;top:100px;width:400px;height:50px;
}

,div {
height:50px;width:100%;
}

/*]]>*/
</style></head>

<body>
<div id="tst" onmouseover="zxcRandomRotate.Pause('tst');" onmouseout="zxcRandomRotate.Auto('tst');">
<div class="div" style="background-Color:red;" >Any HTML 1</div>
<div class="div" style="background-Color:green;" >Any HTML 2</div>
<div class="div" style="background-Color:blue;" >Any HTML 3</div>
</div>
<script> vic=0; </script>
<form name=Show id=Show style="position:absolute;visibility:visible;top:700px;left:0px;" >
<input size=100 name=Show0 >
<input size=10 name=Show1 >
<input size=10 name=Show2 >
<input size=10 name=Show3 >
<input size=10 name=Show4 >
<input size=10 name=Show5 >
<input size=10 name=Show6 >
<input size=10 name=Show7 >
<input size=10 name=Show8 >
<input size=10 name=Show9 ><br>
<textarea name=TA rows=1 cols=100 ></textarea>
</form>

<script type="text/javascript">
/*<![CDATA[*/

var zxcRandomRotate={

init:function(o){
var id=o.ID,hold=o.Hold,obj=document.getElementById(id),clds=obj.childNodes,ary=[],z0=0;
for (;z0<clds.length;z0++){
if (clds[z0].nodeType==1){
clds[z0].style.position='absolute';
clds[z0].style.zIndex='0';
clds[z0].style.left='0px';
clds[z0].style.top='0px';
ary.push(clds[z0]);
}
}
o=this['zxc'+id]={
id:id,
ary:ary,
hold:typeof(hold)=='number'?hold:5000,
nu:-1
}
this.rotate(o);
},

Auto:function(id){
var o=this['zxc'+id],oop=this;
if (o){
o.to=setTimeout(function(){ oop.rotate(o); },500);
}
},

Pause:function(id){
var o=this['zxc'+id];
if (o){
clearTimeout(o.to);
}
},

rotate:function(o){
var nu=Math.floor(Math.random()*o.ary.length),oop=this;
while (nu==o.nu){
nu=Math.floor(Math.random()*o.ary.length);
}
if (o.ary[o.nu]){
o.ary[o.nu].style.zIndex='0';
}
o.ary[nu].style.zIndex='1';
o.nu=nu;
o.to=setTimeout(function(){ oop.Auto(o.id); },o.hold);
}


}

zxcRandomRotate.init({
ID:'tst',
Hold:2000
});

/*]]>*/
</script>

</body>

</html>

Jessic4
11-17-2012, 10:01 AM
Maybe you could save each code snippet into its own txt file and save them as 1.txt, 2.txt and 3.txt, in the same folder.

Then you could use php to randomise the numbers in an include;


<?php
$snippet = array('1', '2', '3');
shuffle($snippet);
include('path/to/snippets/'.$snippet.'.txt');
?>


Same method could be used for randomising images too - just change 'txt' in the code above to 'jpg' or whatever.

This would be perfect. Unfortunately the place I'm trying to run the randomizing script (wordpress plugin toolbar) doesn't seem to display any php include functions. I would try the other javascript suggestion but it's too long for the "message/code to display" text box. Would there be any other possible way?

Beverleyh
11-17-2012, 11:18 AM
I don't use Wordpress a great deal so I'm not sure of the limitation it poses.

Maybe this would be a better question for a Wordpress forum?