collapse collapse

* Simple Portal Archived Forum

This is an Archive Forum.

The content in this forum may be out-of-date or have been superseded by newer information, and links in forum pages to other sites may not work.
This forum contains archives for future reference.

Visit our thread at Simple Machines Forum for current support.

* User Info

 
 
Welcome, Guest. Please login or register.

* Who's Online

  • Dot Guests: 578
  • Dot Hidden: 0
  • Dot Users: 1
  • Dot Users Online:

* Shoutbox

Refresh History
  • Shoutbox is not for support!
  • {OCS}MasterSeal: Yup, Still adore SP
    April 21, 2019, 07:08:06 PM
  • {OCS}MasterSeal: STILL love SP :)
    November 24, 2018, 05:05:50 AM
  • ♦ Ninja ZX-10RR ♦: <3 aegersz
    September 13, 2018, 03:36:09 PM
  • aegersz: I STILL <3 LOVE SimplePortal
    September 13, 2018, 07:11:39 AM
  • aegersz: o LOVE you guys - Simple Portal rocks !
    May 09, 2018, 05:18:59 AM
  • Chen Zhen: our apologies for the site being down.. please read server issues topic
    March 22, 2018, 05:32:38 AM
  • {OCS}MasterSeal: LOL PLEASE forget I just posted that. I found the answer in my own dang post back in 2015. lol sorry!
    July 04, 2017, 10:47:55 PM
  • {OCS}MasterSeal: I know this SB isnt' for support, but I just have a general question. Who would I contact to find out where SP stores its block info? Is it DB driven or files? I searched the site but came up with nothing. probably my fault any insight is appreciated.
    July 04, 2017, 10:43:36 PM
  • ♦ Ninja ZX-10RR ♦: Excuse me but what does Simpleportal have to deal with that?
    February 05, 2017, 08:21:14 PM
  • WhiteEagle: of course IMHO that site appears to be dead :(
    February 04, 2017, 01:08:05 PM
  • WhiteEagle: If I can get that, then I'll use it for that site...
    February 04, 2017, 01:07:35 PM
  • WhiteEagle: decided to not use SMF for any projects, unless I can get a copy of the premium version of the fanfiction archive plugin
    February 04, 2017, 01:06:54 PM
  • expertdecisions: cloudflare
    January 28, 2017, 08:01:47 AM
  • aegersz: SM release 2.0.13 !
    January 12, 2017, 06:00:13 AM
  • raffo: Tks Emanuele, even if I didn't understand the fix :D
    November 07, 2016, 02:01:20 AM
  • emanuele: [link]
    November 01, 2016, 12:43:50 PM
  • emanuele: raffo: the English support board is a good place. ;)
    November 01, 2016, 12:43:38 PM
  • raffo: Where can I find the fix for the shoutbox?
    November 01, 2016, 05:06:09 AM
  • {OCS}MasterSeal: To the SP team, I make a point to come here and thank you as much as possible for your work.  so again, THANK YOU!
    October 28, 2016, 10:38:05 AM
  • emanuele: That's indeed funny, the limit is present only in the patch and not the full install.
    October 22, 2016, 06:14:58 PM

* Recent Posts

Adding Forums Button to Nav bar by jirapon
[August 01, 2019, 09:07:12 AM]


Re: Board Icons by ♦ Ninja ZX-10RR ♦
[July 30, 2019, 04:03:41 PM]


MOVED: Czech translation???? by ♦ Ninja ZX-10RR ♦
[July 30, 2019, 03:04:51 PM]


Board Icons by jirapon
[July 30, 2019, 07:28:44 AM]


Re: Thankyou Simpleportal, by ♦ Ninja ZX-10RR ♦
[July 29, 2019, 09:41:29 AM]

If you're interested in helping other members with support requests, consider joining the Community Support Helpers group.

Author Topic: a random word generator block :-)  (Read 17673 times)

0 Members and 1 Guest are viewing this topic.

Offline Cat McFarlane

  • Semi Newbie
  • *
  • Posts: 9
  • SMF Version: 2.0.5
  • SP Version: 2.3.5
a random word generator block :-)
« 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);

Offline Chen Zhen

  • The Underdog
  • Operations Manager
  • *
  • Posts: 1350
  • Gender: Male
  • Kinesis
    • WebDev
  • SMF Version: 2.1
  • EhPortal Version: 1.22
Re: a random word generator block :-)
« Reply #1 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;

« Last Edit: September 24, 2013, 12:43:51 AM by Underdog »

Offline Cat McFarlane

  • Semi Newbie
  • *
  • Posts: 9
  • SMF Version: 2.0.5
  • SP Version: 2.3.5
Re: a random word generator block :-)
« Reply #2 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
« Last Edit: September 24, 2013, 01:28:05 AM by Cat McFarlane »

Offline ccbtimewiz

  • Hero Member
  • *****
  • Posts: 2185
  • Gender: Male
  • $("div.content:dd").hide();
  • SMF Version: None
  • SP Version: None
  • Elkarte Version: None
  • EhPortal Version: None
Re: a random word generator block :-)
« Reply #3 on: September 24, 2013, 01:27:24 AM »
Can you show me a live demo of the problem?

Offline Chen Zhen

  • The Underdog
  • Operations Manager
  • *
  • Posts: 1350
  • Gender: Male
  • Kinesis
    • WebDev
  • SMF Version: 2.1
  • EhPortal Version: 1.22
Re: a random word generator block :-)
« Reply #4 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;
}

Offline Cat McFarlane

  • Semi Newbie
  • *
  • Posts: 9
  • SMF Version: 2.0.5
  • SP Version: 2.3.5
Re: a random word generator block :-)
« Reply #5 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  :)
« Last Edit: September 24, 2013, 05:35:07 AM by Cat McFarlane »

Offline Cat McFarlane

  • Semi Newbie
  • *
  • Posts: 9
  • SMF Version: 2.0.5
  • SP Version: 2.3.5
Re: a random word generator block :-)
« Reply #6 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  :)

Offline Chen Zhen

  • The Underdog
  • Operations Manager
  • *
  • Posts: 1350
  • Gender: Male
  • Kinesis
    • WebDev
  • SMF Version: 2.1
  • EhPortal Version: 1.22
Re: a random word generator block :-)
« Reply #7 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.
« Last Edit: September 24, 2013, 03:55:51 PM by Underdog »

Offline Chen Zhen

  • The Underdog
  • Operations Manager
  • *
  • Posts: 1350
  • Gender: Male
  • Kinesis
    • WebDev
  • SMF Version: 2.1
  • EhPortal Version: 1.22
Re: a random word generator block :-)
« Reply #8 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>

Offline Cat McFarlane

  • Semi Newbie
  • *
  • Posts: 9
  • SMF Version: 2.0.5
  • SP Version: 2.3.5
Re: a random word generator block :-)
« Reply #9 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.
« Last Edit: September 24, 2013, 09:27:03 PM by Cat McFarlane »

Offline Chen Zhen

  • The Underdog
  • Operations Manager
  • *
  • Posts: 1350
  • Gender: Male
  • Kinesis
    • WebDev
  • SMF Version: 2.1
  • EhPortal Version: 1.22
Re: a random word generator block :-)
« Reply #10 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>

Offline Chen Zhen

  • The Underdog
  • Operations Manager
  • *
  • Posts: 1350
  • Gender: Male
  • Kinesis
    • WebDev
  • SMF Version: 2.1
  • EhPortal Version: 1.22
Re: a random word generator block :-)
« Reply #11 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>
« Last Edit: September 25, 2013, 12:17:51 AM by Underdog »

Offline Cat McFarlane

  • Semi Newbie
  • *
  • Posts: 9
  • SMF Version: 2.0.5
  • SP Version: 2.3.5
Re: a random word generator block :-)
« Reply #12 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?
« Last Edit: September 25, 2013, 09:36:32 AM by Cat McFarlane »

Offline Chen Zhen

  • The Underdog
  • Operations Manager
  • *
  • Posts: 1350
  • Gender: Male
  • Kinesis
    • WebDev
  • SMF Version: 2.1
  • EhPortal Version: 1.22
Re: a random word generator block :-)
« Reply #13 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.

Offline Cat McFarlane

  • Semi Newbie
  • *
  • Posts: 9
  • SMF Version: 2.0.5
  • SP Version: 2.3.5
Re: a random word generator block :-)
« Reply #14 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.
« Last Edit: September 26, 2013, 07:48:00 AM by Cat McFarlane »

Offline Chen Zhen

  • The Underdog
  • Operations Manager
  • *
  • Posts: 1350
  • Gender: Male
  • Kinesis
    • WebDev
  • SMF Version: 2.1
  • EhPortal Version: 1.22
Re: a random word generator block :-)
« Reply #15 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.
« Last Edit: September 26, 2013, 10:13:30 PM by Underdog »

Offline Cat McFarlane

  • Semi Newbie
  • *
  • Posts: 9
  • SMF Version: 2.0.5
  • SP Version: 2.3.5
Re: a random word generator block :-)
« Reply #16 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.  :)

Offline Cat McFarlane

  • Semi Newbie
  • *
  • Posts: 9
  • SMF Version: 2.0.5
  • SP Version: 2.3.5
Re: a random word generator block :-)
« Reply #17 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

Offline Divecall

  • Full Member
  • ***
  • Posts: 201
  • SMF Version: 2.0.2
  • SP Version: 2.3.5
Re: a random word generator block :-)
« Reply #18 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.

Offline Divecall

  • Full Member
  • ***
  • Posts: 201
  • SMF Version: 2.0.2
  • SP Version: 2.3.5
Re: a random word generator block :-)
« Reply #19 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?