Welcome Guest [Log In] [Register]
Search Members Calendar | Rules ZB Code Index IF Code Index
ZBCode
  • Navigation
  • ZBCode
  • Coding Resources
  • Code Requests
  • Completed Requests
  • [Completed] [ZB] Quote Generator (Limited)
Hey, welcome to ZBCode, the premier coding forum for ZB. Here you fill find some of the best Invisionfree and Zetaboards Codes on the network! Unfortunately, you're sorta hovering around in guest mode at the moment; why not join in on the fun? Register an account and you can start accessing the wealth of resources we have available here. Enjoy your stay at ZBCode, and remember to tell all your friends about us; the more members, the more codes available. ;)

Interested in joining? Click here.


If you are already a member of ZBCode, feel free to login right here:

Username:   Password:
Locked Topic
[Completed] [ZB] Quote Generator (Limited)
Tweet Topic Started: Oct 7 2009, 12:40 PM (243 Views)
thebawp Oct 7 2009, 12:40 PM Post #1


Posts:
100
Group:
Member
Member
#2,904
Joined:
May 2, 2009
Okay, I have searched high and low for a script that performs in the way I would like but unfortunately I have come up just a little short.

This is the nearest I can find:

http://javascript.internet.com/games/ask-mustafa.html

I have managed to amend the code slightly to my own needs, but I still can't get it to do exactly what I would like as it requires you to type in your own question and I just want a quote to appear on click. I also know you won't amend a code without permission, so I will explain what I would like and hope someone can duplicate it for me.

Basically, I want my forum members to click either a button or an image and a random quote to be produced (either below or above the button, but preferably in an inscreen pop up if that's possible). I have found scripts that suit this purpose perfectly. However, I would like to limit it so that they can only click the button/image 3 times and then any further clicks will produce a second set of random quotes (i.e. go away now, I'm tired..etc). This way they can't exhaust the random quotes without refreshing the page (if they want to do that, then fine XD). I will be adding this code into a premade webpage on my board (built with the website maker).

Hopefully my explanation is clear enough, if there are any details that need clarifying then please let me know.

Thanks for your time!

Kirsty


Offline Profile Goto Top
 
Reid Oct 7 2009, 04:16 PM Post #2
Member Avatar
What? The land of the free? Whoever told you that was your enemy.

Posts:
1,790
Group:
Distinguished Coder
Member
#148
Joined:
Jul 20, 2008
Try this on your web page:
Code:
 
<script type='text/javascript'>
// <![CDATA[
var levels = [
[ 3, 'first_quote'],
[ 6, 'second_quote'],
[ 9, 'third_quote'],
[ 12, 'fourth_quote']
];
var first_quote = ['1st', '1nd', '1rd', '1th', 'Quite (1)', 'Sometimes (1)', '(1) Hallllo out there' ];
var second_quote = [ 'Etc', 'And so on', "I'm getting tired", 'Life is blue', 'Grass is a pale yellow' ];
var third_quote = [ "Phew. I'm far too tired for this.", 'No you', 'FML', 'MLIA' ];
var fourth_quote = [ 'Nooooo.', 'Noooooooooo.', 'Noooooo!', 'Noo?' ];
// end of options
var number_of_clicks = 0;
jQuery('#r_quote').click(function() {
if (number_of_clicks++ > levels[0][0] && levels.length != 1)
levels.shift();
var x = window[levels[0][1]];
jQuery('#quote_o').html(x[Math.floor(Math.random()*x.length)]);
});
// ]]>
</script>
There are a few things. Firstly, the button to generate another random quote must have an id of r_quote - like this:
Code:
 
<button type='button' id='r_quote'>Another</button>
The place where the quote will be inserted should have an id of quote_o - like this:
Code:
 
<div id='quote_o'>A quote will appear here when you click r_quote</div>
Now for the actual code editing itself... You see the piece of code that says levels? Well, those are the different levels of quote that there are. The first number is how many clicks it will take to move up a level. For example, you will be pulling quotes from first_quote (we'll get to that in a sec) as long as they haven't clicked over 3 times. Once they've clicked the 4th time, it will begin to grab quotes from the next level (in this case, second_quote) until they get over 6 clicks (since that's what the next level says.) Once they get over 6, it'll go to the next level, and so on...

Now the second part of the levels is where it'll grab the quotes from. When they have less than 3 clicks (the first level) then it will pull quotes from the second part of the level. In this case, that's called first_quote. If you look down in the code you'll see where we define first_quote as some quotes. You can add more or less as needed.

Then the second level draws from second_quote and then the third from third_quote and so on and so forth.

If you want to add more levels, then simply add another level on the end like so:
Code:
 
var levels = [
[ 3, 'first_quote'],
[ 6, 'second_quote'],
[ 9, 'third_quote'],
[ 12, 'fourth_quote'],
[ 20000, 'fifth_quote' ]
];
Then you have to define fifth_quote as something. For logic's sense, I'd write it right after fourth_quote, but it doesn't really matter:
Code:
 
var fifth_quote = ['You have far too much time on your hands.', 'Get a life. :P' ];
I realize this is a really sketchy explanation of how the code works, but you couldn't honestly expect it to be simple, could you? xD

Tell me if you need help. :)
The Resource Board
Offline Profile Goto Top
 
Reid Oct 7 2009, 04:18 PM Post #3
Member Avatar
What? The land of the free? Whoever told you that was your enemy.

Posts:
1,790
Group:
Distinguished Coder
Member
#148
Joined:
Jul 20, 2008
Note: If you want it to be a pop-up instead of having it appear on page, change this line:
Code:
 
jQuery('#quote_o').html(x[Math.floor(Math.random()*x.length)]);
to
Code:
 
alert(x[Math.floor(Math.random()*x.length)]);
The Resource Board
Offline Profile Goto Top
 
thebawp Oct 7 2009, 06:13 PM Post #4


Posts:
100
Group:
Member
Member
#2,904
Joined:
May 2, 2009
Absolutely awesome. You've actually made this as flexible as I would have liked though I hardly dare ask for the extra stuff! I think the only problem I had was remembering to put " " around a quote that had an apostrophe in it.

Stunning, thank you so much. =)

Actually, is it possible to use this code to redirect someone if they click so many times? Could be a fun little easter egg!

So, for example, when they reach level 4, instead of receiving a message, they are sent to a different page?
Edited by thebawp, Oct 7 2009, 06:14 PM.
Offline Profile Goto Top
 
Reid Oct 7 2009, 06:27 PM Post #5
Member Avatar
What? The land of the free? Whoever told you that was your enemy.

Posts:
1,790
Group:
Distinguished Coder
Member
#148
Joined:
Jul 20, 2008
Try this instead, then:
Code:
 
<script type='text/javascript'>
// <![CDATA[
var levels = [
[ 3, 'first_quote'],
[ 6, 'second_quote'],
[ 9, 'third_quote'],
[ 12, 'fourth_quote']
];
var first_quote = ['1st', '1nd', '1rd', '1th', 'Quite (1)', 'Sometimes (1)', '(1) Hallllo out there' ];
var second_quote = [ 'Etc', 'And so on', "I'm getting tired", 'Life is blue', 'Grass is a pale yellow' ];
var third_quote = [ "Phew. I'm far too tired for this.", 'No you', 'FML', 'MLIA' ];
var fourth_quote = [ 'Nooooo.', 'Noooooooooo.', 'Noooooo!', 'Noo?' ];
// end of options
var number_of_clicks = 0;
jQuery('#r_quote').click(function() {
if (number_of_clicks++ > levels[0][0] && levels.length != 1)
levels.shift();
if (typeof(window[levels[0][1]]) != 'undefined')
var x = window[levels[0][1]];
else if (levels[0][1].indexOf('http://') != -1) {
location.href = levels[0][1];
return;
}
jQuery('#quote_o').html(x[Math.floor(Math.random()*x.length)]);
});
// ]]>
</script>
Instead of putting first_quote or something, just put a url there instead. Be sure it's a full, good URL - with the http:// and everything.

Edit: Fixed a bug

Edit 2: Cleaned it up a bit
Edited by Reid, Oct 7 2009, 06:35 PM.
The Resource Board
Offline Profile Goto Top
 
thebawp Oct 8 2009, 01:54 AM Post #6


Posts:
100
Group:
Member
Member
#2,904
Joined:
May 2, 2009
Well, that's just working perfectly by the looks of things! Thank you so much! I think this can be closed now.

Thanks again!
Offline Profile Goto Top
 
Reid Oct 8 2009, 03:59 PM Post #7
Member Avatar
What? The land of the free? Whoever told you that was your enemy.

Posts:
1,790
Group:
Distinguished Coder
Member
#148
Joined:
Jul 20, 2008
You're welcome.
See? We can read thoughts too. ;)

Your request has been completed, and we're moving it to the correct forum. We hope it works for you; if it doesn't, feel free to post another request topic in the requests forum. Just remember: read the rules first!

Thanks,
The ZBCode Staff
The Resource Board
Offline Profile Goto Top
 
1 user reading this topic (1 Guest and 0 Anonymous)
« Previous Topic · Completed Requests · Next Topic »
Locked Topic

Track Topic · E-mail Topic Time: 2:27 PM Jul 11
Hosted for free by ZetaBoards · Privacy Policy