Random Topic JS

Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Reply

Join Date: Oct 2005
Posts: 240
Reputation: Inny is an unknown quantity at this point 
Solved Threads: 6
Inny's Avatar
Inny Inny is offline Offline
Posting Whiz in Training

Random Topic JS

 
0
  #1
Apr 18th, 2008
Is there a better way to write this short code? It goes to topics that dont exist, I want it to check first so that dosent happen, and not to go to topics in a particular forum, a forum url looks like this (last number being the forum id)
http://inny.ipbfree.com/index.php?showforum=1

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <center><script>
  2. //rough estimate of topics on your forum
  3. topiccount = 2000
  4. topicnum = Math.floor(Math.random()*topiccount)
  5. document.write("<a href='http://inny.ipbfree.com/index.php?showtopic=" + topicnum + "'><img src=http://www.parexcellence.co.uk/weblink/images/randomlink.gif border=0></a>")
  6. </script>
  7. </center>

any ideas?
Always carry a flagon of whiskey in case of snakebite and furthermore always carry a small snake.
W. C. Fields
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 24
Reputation: HenryGR is an unknown quantity at this point 
Solved Threads: 4
HenryGR's Avatar
HenryGR HenryGR is offline Offline
Newbie Poster

Re: Random Topic JS

 
0
  #2
Apr 20th, 2008
Try to change the random generation to an expression like:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. topicnum = Math.round(Math.random()*(topiccount-1))+0;
You keep going, have a Nice day!
Henry.

Before printing this message, make sure is necessary.
Reply With Quote Quick reply to this message  
Join Date: Oct 2005
Posts: 240
Reputation: Inny is an unknown quantity at this point 
Solved Threads: 6
Inny's Avatar
Inny Inny is offline Offline
Posting Whiz in Training

Re: Random Topic JS

 
0
  #3
Apr 20th, 2008
Thanks But it still goes to non existant links.
Is there some way to make it check first that the topic exist?
Always carry a flagon of whiskey in case of snakebite and furthermore always carry a small snake.
W. C. Fields
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 24
Reputation: HenryGR is an unknown quantity at this point 
Solved Threads: 4
HenryGR's Avatar
HenryGR HenryGR is offline Offline
Newbie Poster

Re: Random Topic JS

 
0
  #4
Apr 20th, 2008
Are you sure that you have 2000 topics? Or is that only the maximum number?
You would need to know the maximum number of topics for the random generation number to work between the range. So, in the expression:

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. topicnum = Math.round(Math.random()*(topiccount-1))+0;

"topiccount" must be the maximum o available topics.
You keep going, have a Nice day!
Henry.

Before printing this message, make sure is necessary.
Reply With Quote Quick reply to this message  
Join Date: Oct 2005
Posts: 240
Reputation: Inny is an unknown quantity at this point 
Solved Threads: 6
Inny's Avatar
Inny Inny is offline Offline
Posting Whiz in Training

Re: Random Topic JS

 
0
  #5
Apr 20th, 2008
I have just uinder 2000 topics but that changes everyday. It may go to something like topic number 1876 which may have been deleted or may not exist yet. This will trigger my error page. Id rather that didnt happen because it gives the impression that something is broken
Last edited by Inny; Apr 20th, 2008 at 8:36 am.
Always carry a flagon of whiskey in case of snakebite and furthermore always carry a small snake.
W. C. Fields
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 24
Reputation: HenryGR is an unknown quantity at this point 
Solved Threads: 4
HenryGR's Avatar
HenryGR HenryGR is offline Offline
Newbie Poster

Re: Random Topic JS

 
0
  #6
Apr 20th, 2008
So if I understand correctly, there is not a problem with the random generation but with the links; those, you have to include some kind of check before you write the link on your page, something like
...
get the number
check for existence
go back to check if does not exists
write the link
...

do you hold a record of existing/active topics?
You keep going, have a Nice day!
Henry.

Before printing this message, make sure is necessary.
Reply With Quote Quick reply to this message  
Join Date: Oct 2005
Posts: 240
Reputation: Inny is an unknown quantity at this point 
Solved Threads: 6
Inny's Avatar
Inny Inny is offline Offline
Posting Whiz in Training

Re: Random Topic JS

 
0
  #7
Apr 20th, 2008
I have Logs of Existing Topics yes..
the javascript for it (search stats) seems to be (url removed for security reasons)

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <script type='text/javascript'>
  2. function dojump()
  3.  
  4. {
  5.  
  6. if ( document.jumpmenu.val.options[document.jumpmenu.val.selectedIndex].value != '' )
  7.  
  8. {
  9.  
  10. window.location.href = 'url removed'+ '&' + document.jumpmenu.val.options[document.jumpmenu.val.selectedIndex].value;
  11. }
  12. }
  13. </script>
Always carry a flagon of whiskey in case of snakebite and furthermore always carry a small snake.
W. C. Fields
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 24
Reputation: HenryGR is an unknown quantity at this point 
Solved Threads: 4
HenryGR's Avatar
HenryGR HenryGR is offline Offline
Newbie Poster

Re: Random Topic JS

 
0
  #8
Apr 20th, 2008
if "selectedIndex" within your function is the topic than can exist or not, I would create a function to find out the correct random destination, something like:

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. function destination() {
  2.  
  3. var urltogo = '';
  4.  
  5. while (urltogo == '')
  6. {
  7. selectedIndex = Math.round(Math.random()*(topiccount-1))+0;
  8. urltogo = document.jumpmenu.val.options[document.jumpmenu.val.selectedIndex].value;
  9. }
  10.  
  11. return urltogo;
  12. }
You keep going, have a Nice day!
Henry.

Before printing this message, make sure is necessary.
Reply With Quote Quick reply to this message  
Join Date: Oct 2005
Posts: 240
Reputation: Inny is an unknown quantity at this point 
Solved Threads: 6
Inny's Avatar
Inny Inny is offline Offline
Posting Whiz in Training

Re: Random Topic JS

 
0
  #9
Apr 20th, 2008
Thanks So much For your Help On this. Think I have it now.
While your online mate, Do you think you could help with this one? (see last post)

http://www.daniweb.com/forums/thread113427.html

Much Appreciated!
Always carry a flagon of whiskey in case of snakebite and furthermore always carry a small snake.
W. C. Fields
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the JavaScript / DHTML / AJAX Forum


Views: 1132 | Replies: 8
Thread Tools Search this Thread



Tag cloud for JavaScript / DHTML / AJAX
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC