SimplePortal

Development => Feature Requests => Topic started by: Cat McFarlane on September 23, 2013, 09:57:37 PM

Title: a random word generator block :-)
Post by: Cat McFarlane on September 23, 2013, 09:57:37 PM
Hi, was advised about some code for a word generator, which people on my art forum love as drawing inspiration, and which I can base art games around. Have put it into an html simpleportal block. This is the text (519 words entered alphabetically, so far). Issue I have is that often the words are coming up in alphabetical order rather than really randomly, and I want to be sure the art games don't get stuck on a loop, with everyone only getting early words and the same alphabetical words etc.

Also, the box shown in pic comes up after a few clicks, and I don't want people clicking on it, as that stops the generator working ... not sure if only I can see that, but just to be safe.

Thanks

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Random thingy</title>

<script language type="text/javascript">
<!--

function getMessage()
{
var ar = new Array(519)
ar[0] = ... 519 alphabetically here


var now = new Date()
var sec = now.getSeconds()
alert("Random thingies:\n\n" + ar[sec % 519])
}

//-->
</script>

</head>

<form>
<input type="button" name="again" value="Generate another"

onClick="getMessage()">
</form>

</body>
</html>

EDIT: tried incorporating the following instead ...

<script language="JavaScript">
<!--
var randomString = new Array ();
randomString[0] = "a random string";
randomString[1] = "another random string";
randomString[2] = "another random string";
randomString[3] = "another random string";
randomString[4] = "another random string";
randomString[5] = "another random string";
randomString[6] = "another random string";
randomString[7] = "another random string";
var i = Math.floor(7*Math.random())

document.write(randomString);
//-->
</script>

but same alphabetized results, not random across entire 519 words.

Had put at bottom of list ... var i = Math.floor((Math.random()*519)+1);
Title: Re: a random word generator block :-)
Post by: Chen Zhen on September 23, 2013, 11:42:57 PM
Here is a javacript version of what was requested:

Code: [Select]
<script language type="text/javascript"><!--
function getMessage()
{
var ar = new Array(
"test1",
"test2",
"test3",
"test4",
"test5");
alert("Random thingies:\n\n" + ar[Math.floor(Math.random()*(ar.length))]);
}
//--></script>
<form>
<input type="button" name="again" value="Generate another" onclick="getMessage()" />
</form>



Using PHP, you might try something like this:

Code: [Select]
$ar = array(
"test1",
"test2",
"test3",
"test4",
"test5");

echo '
<form action="',currentPageUrl(),'" method="post">
<input type="submit" id="wordgenerator" name="wordgen" value="Generate another" />
<input type="hidden" name="submit_word" />
</form>';

if(isset($_REQUEST['wordgen']) && isset($_REQUEST['submit_word']))
echo '<br />Random thingies:<br /><br /> ' . $ar[mt_rand(0, (count($ar)-1))];

function currentPageUrl($pageURL = 'http://')
{           
if (!empty($_SERVER["HTTPS"]))
$pageURL = "https://";   
   
if ($_SERVER["SERVER_PORT"] != "80")
$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
else
$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
   
return $pageURL;

Title: Re: a random word generator block :-)
Post by: Cat McFarlane on September 24, 2013, 01:01:33 AM
Here is a javacript version of what was requested:

Code: [Select]
<script language type="text/javascript"><!--
function getMessage()
{
var ar = new Array(
"test1",
"test2",
"test3",
"test4",
"test5");
alert("Random thingies:\n\n" + ar[Math.floor(Math.random()*(ar.length))]);
}
//--></script>
<form>
<input type="button" name="again" value="Generate another" onclick="getMessage()" />
</form>



Using PHP, you might try something like this:

Code: [Select]
$ar = array(
"test1",
"test2",
"test3",
"test4",
"test5");

echo '
<form action="',currentPageUrl(),'" method="post">
<input type="submit" id="wordgenerator" name="wordgen" value="Generate another" />
<input type="hidden" name="submit_word" />
</form>';

if(isset($_REQUEST['wordgen']) && isset($_REQUEST['submit_word']))
echo '<br />Random thingies:<br /><br /> ' . $ar[mt_rand(0, (count($ar)-1))];

function currentPageUrl($pageURL = 'http://')
{           
if (!empty($_SERVER["HTTPS"]))
$pageURL = "https://";   
   
if ($_SERVER["SERVER_PORT"] != "80")
$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
else
$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
   
return $pageURL;


Thanks so much, Underdog.   :)  I've got the block set as a left block, and have noticed that some changes made in other boxes on the portal page cause changes in other boxes, so am wondering if it could be that, as the first code meant that when I clicked on the button there wasn't a word generated (did I need to number each word?), and the second cut off all boxes on the rest of the page underneath that word, and seemed to be reading off the other word list, even though that wasn't active on the page at the time. And logo.jpg I added at top of page, in theme (Fresh Looks) keeps going back to tiny version/have to re-upload. 519 words, and just shows early ones. 
Thanks, Cat
Title: Re: a random word generator block :-)
Post by: ccbtimewiz on September 24, 2013, 01:27:24 AM
Can you show me a live demo of the problem?
Title: Re: a random word generator block :-)
Post by: Chen Zhen on September 24, 2013, 01:44:34 AM

  Here is another PHP example that will not pick the same choice twice (until the loop is reset). Replace the array text to your own entries and change the echo to how you wish it to display.

Code: [Select]
$ar = array(
"test1",
"test2",
"test3",
"test4",
"test5"
);

echo '
<form action="', currentPageUrl(), '" method="post">
<input type="submit" id="wordgenerator" name="wordgen" value="Generate another" />
<input type="hidden" name="submit_word" />
</form>';

if(isset($_REQUEST['wordgen']) && isset($_REQUEST['submit_word']))
{
shuffle($ar);
if (empty($_SESSION['wordgenerator']) ? 0 : $_SESSION['wordgenerator'] >= count($ar))
{
$_SESSION['wordgenerator'] = 0;
foreach ($ar as $word)
$_SESSION['wordgen'][$word] = false;
}

foreach ($ar as $generated_word)
{
if (empty($_SESSION['wordgen'][$generated_word]))
{
$_SESSION['wordgen'][$generated_word] = true;
$_SESSION['wordgenerator'] = !empty($_SESSION['wordgenerator']) ? $_SESSION['wordgenerator'] + 1 : 1;
echo '<br />Random thingies:<br /><br /> ' . $generated_word;
break;
}
}
}

function currentPageUrl($pageURL = 'http://')
{
if (!empty($_SERVER["HTTPS"]))
$pageURL = "https://";

if ($_SERVER["SERVER_PORT"] != "80")
$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
else
$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];

return $pageURL;
}
Title: Re: a random word generator block :-)
Post by: Cat McFarlane on September 24, 2013, 02:47:55 AM

  Here is another PHP example that will not pick the same choice twice (until the loop is reset). Replace the array text to your own entries and change the echo to how you wish it to display.

Code: [Select]
$ar = array(
"test1",
"test2",
"test3",
"test4",
"test5"
);

echo '
<form action="', currentPageUrl(), '" method="post">
<input type="submit" id="wordgenerator" name="wordgen" value="Generate another" />
<input type="hidden" name="submit_word" />
</form>';

if(isset($_REQUEST['wordgen']) && isset($_REQUEST['submit_word']))
{
shuffle($ar);
if (empty($_SESSION['wordgenerator']) ? 0 : $_SESSION['wordgenerator'] >= count($ar))
{
$_SESSION['wordgenerator'] = 0;
foreach ($ar as $word)
$_SESSION['wordgen'][$word] = false;
}

foreach ($ar as $generated_word)
{
if (empty($_SESSION['wordgen'][$generated_word]))
{
$_SESSION['wordgen'][$generated_word] = true;
$_SESSION['wordgenerator'] = !empty($_SESSION['wordgenerator']) ? $_SESSION['wordgenerator'] + 1 : 1;
echo '<br />Random thingies:<br /><br /> ' . $generated_word;
break;
}
}
}

function currentPageUrl($pageURL = 'http://')
{
if (!empty($_SERVER["HTTPS"]))
$pageURL = "https://";

if ($_SERVER["SERVER_PORT"] != "80")
$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
else
$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];

return $pageURL;
}

Hi Underdog :-)

That embeds great, and thank you very much for the code. Is there any way to make it a pop up button, instead of page refresh. It won't go past 'a' or 'b' words ... is that to do with refresh rate or something else, as am really puzzled why the whole list isn't being accessed, no matter what code is used, plus eg page banner made small again. There'll be about 2000 words in total; got about 400 in there right now. Just realised too that there does need to be repeat, with lots using and for various games. Sorry, entirely new to this.

Thanks  :)
Title: Re: a random word generator block :-)
Post by: Cat McFarlane on September 24, 2013, 02:49:20 AM
Can you show me a live demo of the problem?

Hi Bullet :-)

Do you mean you want me to video it, or to set up a username/password?

Thanks, Cat  :)
Title: Re: a random word generator block :-)
Post by: Chen Zhen on September 24, 2013, 03:33:43 PM
Cat McFarlane,
 
  Here is another that uses php and a js alert box although it still refreshes the page.

Code: [Select]
$ar = array(
"test1",
"test2",
"test3",
"test4",
"test5"
);

echo '
<form action="', currentPageUrl(), '" method="post">
<input type="submit" id="wordgenerator" name="wordgen" value="Generate another" />
<input type="hidden" name="submit_word" />
</form>';

if(isset($_REQUEST['wordgen']) && isset($_REQUEST['submit_word']))
{
shuffle($ar);
if (empty($_SESSION['wordgenerator']) ? 0 : $_SESSION['wordgenerator'] >= count($ar))
{
$_SESSION['wordgenerator'] = 0;
foreach ($ar as $word)
$_SESSION['wordgen'][$word] = false;
}

foreach ($ar as $generated_word)
{
if (empty($_SESSION['wordgen'][$generated_word]))
{
$_SESSION['wordgen'][$generated_word] = true;
$_SESSION['wordgenerator'] = !empty($_SESSION['wordgenerator']) ? $_SESSION['wordgenerator'] + 1 : 1;
echo '
<script type="text/javascript"><!-- // --><![CDATA[
window.onload = function() {
var generated_word = "', $generated_word,'";
alert("Random thingies:\n\n" + generated_word);
}
// ]]></script>';
break;
}
}
}

function currentPageUrl($pageURL = 'http://')
{
if (!empty($_SERVER["HTTPS"]))
$pageURL = "https://";

if ($_SERVER["SERVER_PORT"] != "80")
$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
else
$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];

return $pageURL;
}

  I will create a block that does not need to refresh the page when I have some time today.
Title: Re: a random word generator block :-)
Post by: Chen Zhen on September 24, 2013, 06:43:04 PM
Cat McFarlane,

  Here is an example using javascript and a bit of HTML for the form of course. It shuffles the array and then displays the words without repeating (until the loop resets) as the user clicks the button. This will not reload the page.

Code: [Select]
<script type="text/javascript"><!-- // --><![CDATA[
var mytext1 = new Array(
"test1",
"test2",
"test3",
"test4",
"test5"
);
var countx = 0;
var mytext = shuffle(mytext1);

function getMessage(display_word)
{
countx = countx + 1;
if (countx >= mytext.length)
countx = 0;

alert("Random thingies:\n\n" + display_word);
}

function shuffle(wordx)
{
for(var j, x, i = wordx.length; i; j = Math.floor(Math.random() * i), x = wordx[--i], wordx[i] = wordx[j], wordx[j] = x);
return wordx;
}
// ]]></script>
<form>
<input type="button" name="again" value="Generate another" onclick="getMessage(mytext[countx])" />
</form>
Title: Re: a random word generator block :-)
Post by: Cat McFarlane on September 24, 2013, 07:14:08 PM
Cat McFarlane,

  Here is an example using javascript and a bit of HTML for the form of course. It shuffles the array and then displays the words without repeating (until the loop resets) as the user clicks the button. This will not reload the page.

Code: [Select]
<script type="text/javascript"><!-- // --><![CDATA[
var mytext1 = new Array(
"test1",
"test2",
"test3",
"test4",
"test5"
);
var countx = 0;
var mytext = shuffle(mytext1);

function getMessage(display_word)
{
countx = countx + 1;
if (countx >= mytext.length)
countx = 0;

alert("Random thingies:\n\n" + display_word);
}

function shuffle(wordx)
{
for(var j, x, i = wordx.length; i; j = Math.floor(Math.random() * i), x = wordx[--i], wordx[i] = wordx[j], wordx[j] = x);
return wordx;
}
// ]]></script>
<form>
<input type="button" name="again" value="Generate another" onclick="getMessage(mytext[countx])" />
</form>

Thank you so much for taking the time to provide this. I really appreciate. Will try it out now.
Thanks  :)

EDIT: works great, thanks! Wonderful to be able to use this for drawing games;sure everyone will enjoy it very much ... already people posting about how much they like it. Really appreciate :-)
Loop broken is page refresh, so it starts accessing full list again?
And is it possible to stop the checkbox option appearing, as anyone clicking that is going to stop their accessibility to words, and some on the forum could easily do that. I noticed in IE that a triangle showed up instead. If I know where to edit to stop that checkbox and message coming up, that would be awesome.
Thanks :-)

And is there any quick way to edit this ...

celery,2,0
cemetery,2,1
centaur,3,0
center,2,0
cereal,2,1
chain,1,0
chainsaw,2,1
chair,1,0
chalk,2,0

to having the quotes and comma?
100's and 100's to do, lol.
And read can store offline, so server not having to bring up 1000's of words each time refreshes. Sorry for all questions.
Title: Re: a random word generator block :-)
Post by: Chen Zhen on September 24, 2013, 11:31:28 PM
Cat McFarlane,

  With this code it remembers the array in a cookie that the user's browser will remember until their browser is closed or its history is erased.

  alert and confirm do not allow the removal of the checkbox. The only way to not have that behavior is to come up with your own way of displaying the information. With the code below it will simply display the info on the right hand side of the block.


Code: [Select]
<script type="text/javascript"><!-- // --><![CDATA[
if (GetCookie("mywords") == null)
{
var mytext1 = new Array(
"test1",
"test2",
"test3",
"test4",
"test5"
);
var mytext = shuffle(mytext1);
document.cookie = "mywords=" + escape(mytext.join("~"));
}
else
var mytext = GetCookie("mywords").split("~");

var countx = 0;
if (GetCookie("ycount") == null)
document.cookie = "ycount=0";
else
countx = GetCookie("ycount");

function getMessage(display_word)
{
countx = countx + 1;
                document.cookie = "ycount=" + escape(countx);
if (countx >= mytext.length)
{
countx = 0;
document.cookie = "ycount=0";
}
document.getElementById("myword").innerHTML = display_word;
}

function shuffle(wordx)
{
for(var j, x, i = wordx.length; i; j = Math.floor(Math.random() * i), x = wordx[--i], wordx[i] = wordx[j], wordx[j] = x);
return wordx;
}

function GetCookie(sName)
{
var aCookie = document.cookie.split("; ");
for (var i=0; i < aCookie.length; i++)
{   
var aCrumb = aCookie[i].split("=");
if (sName == aCrumb[0])
return unescape(aCrumb[1]);
}
 
return null;
}
// ]]></script>
<form>
<span>
<input type="button" name="again" value="Generate another" onclick="getMessage(mytext[countx])" />
</span>
<span style="float:right" id="myword">
&nbsp;
</span>
</form>
Title: Re: a random word generator block :-)
Post by: Chen Zhen on September 24, 2013, 11:59:27 PM
Cat McFarlane,

  Try this block that will display your word in a container in the top-right corner and will not display after 40000 inactive ms.


Code: [Select]
<script type="text/javascript"><!-- // --><![CDATA[
if (GetCookie("mywords") == null)
{
var mytext1 = new Array(
"test1",
"test2",
"test3",
"test4",
"test5"
);
var mytext = shuffle(mytext1);
document.cookie = "mywords=" + escape(mytext.join("~"));
}
else
var mytext = GetCookie("mywords").split("~");

var countx = 0;
if (GetCookie("ycount") == null)
document.cookie = "ycount=0";
else
countx = GetCookie("ycount");

function getMessage(display_word)
{
countx = countx + 1;
                document.cookie = "ycount=" + escape(countx);

if (display_word == null)
display_word = mytext[0];

if (countx >= mytext.length)
{
countx = 0;
document.cookie = "ycount=0";
}
                document.getElementById("myword").style.display = "block";
document.getElementById("myword").innerHTML = display_word;

setTimeout(function()
{
document.getElementById("myword").style.display = "none";
}, 40000);
}

function shuffle(wordx)
{
for(var j, x, i = wordx.length; i; j = Math.floor(Math.random() * i), x = wordx[--i], wordx[i] = wordx[j], wordx[j] = x);
return wordx;
}

function GetCookie(sName)
{
var aCookie = document.cookie.split("; ");
for (var i=0; i < aCookie.length; i++)
{
var aCrumb = aCookie[i].split("=");
if (sName == aCrumb[0])
return unescape(aCrumb[1]);
}

return null;
}
// ]]></script>
<form>
<span>
<input type="button" name="again" value="Generate another" onclick="getMessage(mytext[countx])" />
</span>
<span style="display:none;position: fixed;top: 0px;right: 0px;padding:15px;margin:1em 0 3em;color:#fff;max-width: 400px;border: 1px solid #222222;border-radius:10px;box-shadow: 3px 3px 39px 2px #222222;background-color: #2989d8;" id="myword">
&nbsp;
</span>
</form>
Title: Re: a random word generator block :-)
Post by: Cat McFarlane on September 25, 2013, 08:12:17 AM
Cat McFarlane,

  With this code it remembers the array in a cookie that the user's browser will remember until their browser is closed or its history is erased.

  alert and confirm do not allow the removal of the checkbox. The only way to not have that behavior is to come up with your own way of displaying the information. With the code below it will simply display the info on the right hand side of the block.


Code: [Select]
<script type="text/javascript"><!-- // --><![CDATA[
if (GetCookie("mywords") == null)
{
var mytext1 = new Array(
"test1",
"test2",
"test3",
"test4",
"test5"
);
var mytext = shuffle(mytext1);
document.cookie = "mywords=" + escape(mytext.join("~"));
}
else
var mytext = GetCookie("mywords").split("~");

var countx = 0;
if (GetCookie("ycount") == null)
document.cookie = "ycount=0";
else
countx = GetCookie("ycount");

function getMessage(display_word)
{
countx = countx + 1;
                document.cookie = "ycount=" + escape(countx);
if (countx >= mytext.length)
{
countx = 0;
document.cookie = "ycount=0";
}
document.getElementById("myword").innerHTML = display_word;
}

function shuffle(wordx)
{
for(var j, x, i = wordx.length; i; j = Math.floor(Math.random() * i), x = wordx[--i], wordx[i] = wordx[j], wordx[j] = x);
return wordx;
}

function GetCookie(sName)
{
var aCookie = document.cookie.split("; ");
for (var i=0; i < aCookie.length; i++)
{   
var aCrumb = aCookie[i].split("=");
if (sName == aCrumb[0])
return unescape(aCrumb[1]);
}
 
return null;
}
// ]]></script>
<form>
<span>
<input type="button" name="again" value="Generate another" onclick="getMessage(mytext[countx])" />
</span>
<span style="float:right" id="myword">
&nbsp;
</span>
</form>

Hugest thanks to you, Underdog ... that is absolutely perfect! Really appreciate very much, and am so excited about starting off the big art drawing game soon ... thanks for making that possible for everyone ... hats off to you!
Thanks, Cat  :applause:  :)

EDIT: eek, it's stopped working ... 1636 words in the list (another 1000 to go) ... will it have stopped due to server issues?
Title: Re: a random word generator block :-)
Post by: Chen Zhen on September 25, 2013, 09:17:38 PM
Cat McFarlane, 

  Perhaps you keyed something incorrect into the array at that point? Double check your edits to the code.
Title: Re: a random word generator block :-)
Post by: Cat McFarlane on September 25, 2013, 10:50:59 PM
Cat McFarlane, 

  Perhaps you keyed something incorrect into the array at that point? Double check your edits to the code.

Thanks Underdog; will double check and post back. :-)

EDIT: 4 errors! Hit '2' instead of '"' ... all fixed now! Thanks so much again. At what point would it be best to consider server-based list? If the most words would be eg 6000, that would fall under the bar still?  :) 'Undefined' error this morning (UK) was one teeny comma,lol. Word Generator is working great.
Title: Re: a random word generator block :-)
Post by: Chen Zhen on September 26, 2013, 05:45:28 PM
Cat McFarlane,

  I am glad that worked out for you. Here is another html block that has a closing [ x ] link added to it.

HTML block code:
Code: [Select]

<script type="text/javascript"><!-- // --><![CDATA[
var spanx = '<span id="spanx" style="position:absolute;top: 0px;right: 1px;font-size:8px;"><a style="text-decoration:none;" onclick="closelink()" onmouseover="overlink()" onmouseout="outlink()">[x]</a></span>';

if (GetCookie("mywords") == null)
{
var mytext1 = new Array(
"test1",
"test2",
"test3",
"test4",
"test5"
);
var mytext = shuffle(mytext1);
document.cookie = "mywords=" + escape(mytext.join("~"));
}
else
var mytext = GetCookie("mywords").split("~");

var countx = 0;
if (GetCookie("ycount") == null)
document.cookie = "ycount=0";
else
countx = GetCookie("ycount");

function getMessage(display_word)
{
this.clearTimeout(myTimeoutx);
countx = countx + 1;
                document.cookie = "ycount=" + escape(countx);

if (display_word == null)
display_word = mytext[0];

if (countx >= mytext.length)
{
countx = 0;
document.cookie = "ycount=0";
}
                document.getElementById("myword").style.display = "block";
document.getElementById("myword").innerHTML = display_word + spanx;

var myTimeoutx = setTimeout(function()
{
closelink();
}, 40000);
}

function shuffle(wordx)
{
for(var j, x, i = wordx.length; i; j = Math.floor(Math.random() * i), x = wordx[--i], wordx[i] = wordx[j], wordx[j] = x);
return wordx;
}

function GetCookie(sName)
{
var aCookie = document.cookie.split("; ");
for (var i=0; i < aCookie.length; i++)
{
var aCrumb = aCookie[i].split("=");
if (sName == aCrumb[0])
return unescape(aCrumb[1]);
}

return null;
}

function closelink()
{
document.getElementById("myword").style.display = "none";
}

function overlink()
{
document.getElementById("spanx").style.fontWeight = "bold";
}

function outlink()
{
document.getElementById("spanx").style.fontWeight = "normal";
}
// ]]></script>
<form>
<span>
<input type="button" name="again" value="Generate another" onclick="getMessage(mytext[countx]);return false;" />
</span>
<span style="display:none;position: fixed;top: 0px;right: 0px;padding:15px;margin:1em 0 3em;color:#fff;max-width: 400px;border: 1px solid #222222;border-radius:10px;box-shadow: 3px 3px 39px 2px #222222;background-color: #2989d8;" id="myword">
&nbsp;
</span>
</form>

Edit -> Timeout had to be reset each time.
Title: Re: a random word generator block :-)
Post by: Cat McFarlane on October 01, 2013, 06:16:33 AM
Cat McFarlane,

  I am glad that worked out for you. Here is another html block that has a closing [ x ] link added to it.

HTML block code:
Code: [Select]

<script type="text/javascript"><!-- // --><![CDATA[
var spanx = '<span id="spanx" style="position:absolute;top: 0px;right: 1px;font-size:8px;"><a style="text-decoration:none;" onclick="closelink()" onmouseover="overlink()" onmouseout="outlink()">[x]</a></span>';

if (GetCookie("mywords") == null)
{
var mytext1 = new Array(
"test1",
"test2",
"test3",
"test4",
"test5"
);
var mytext = shuffle(mytext1);
document.cookie = "mywords=" + escape(mytext.join("~"));
}
else
var mytext = GetCookie("mywords").split("~");

var countx = 0;
if (GetCookie("ycount") == null)
document.cookie = "ycount=0";
else
countx = GetCookie("ycount");

function getMessage(display_word)
{
this.clearTimeout(myTimeoutx);
countx = countx + 1;
                document.cookie = "ycount=" + escape(countx);

if (display_word == null)
display_word = mytext[0];

if (countx >= mytext.length)
{
countx = 0;
document.cookie = "ycount=0";
}
                document.getElementById("myword").style.display = "block";
document.getElementById("myword").innerHTML = display_word + spanx;

var myTimeoutx = setTimeout(function()
{
closelink();
}, 40000);
}

function shuffle(wordx)
{
for(var j, x, i = wordx.length; i; j = Math.floor(Math.random() * i), x = wordx[--i], wordx[i] = wordx[j], wordx[j] = x);
return wordx;
}

function GetCookie(sName)
{
var aCookie = document.cookie.split("; ");
for (var i=0; i < aCookie.length; i++)
{
var aCrumb = aCookie[i].split("=");
if (sName == aCrumb[0])
return unescape(aCrumb[1]);
}

return null;
}

function closelink()
{
document.getElementById("myword").style.display = "none";
}

function overlink()
{
document.getElementById("spanx").style.fontWeight = "bold";
}

function outlink()
{
document.getElementById("spanx").style.fontWeight = "normal";
}
// ]]></script>
<form>
<span>
<input type="button" name="again" value="Generate another" onclick="getMessage(mytext[countx]);return false;" />
</span>
<span style="display:none;position: fixed;top: 0px;right: 0px;padding:15px;margin:1em 0 3em;color:#fff;max-width: 400px;border: 1px solid #222222;border-radius:10px;box-shadow: 3px 3px 39px 2px #222222;background-color: #2989d8;" id="myword">
&nbsp;
</span>
</form>

Edit -> Timeout had to be reset each time.

Thanks so much, Underdog. My apologies in not getting back to you sooner; not been too well. Really appreciate.  :)
Title: Re: a random word generator block :-)
Post by: Cat McFarlane on October 02, 2013, 05:56:13 AM
Hi again :-)

I wondered if I could ask how I adapt the code in order to have a few sentences of info per array, that appear in the box, without a button. Would love to display a little description of art history facts. Tried myself, but goofed it.

Thanks so much, Cat
Title: Re: a random word generator block :-)
Post by: Divecall on October 06, 2013, 12:07:00 PM
Hi !

I want to use this code-snippet, for to show an "article of the day" from my wiki.

Want i need to enter in the array, for to show links like:

Code: [Select]
[url=http://mywikipath.com/articlename]SHOWED ARTICLENAME[/url]
Thanks in advance!

D.
Title: Re: a random word generator block :-)
Post by: Divecall on October 06, 2013, 02:32:48 PM
ohh...

and is it possible to put the output from the code inside from a block, not in a new pop-up window?
SimplePortal 2.3.8 © 2008-2024, SimplePortal