943,621 Members | Top Members by Rank

Ad:
Apr 18th, 2008
0

Random Topic JS

Expand Post »
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?
Similar Threads
Reputation Points: 11
Solved Threads: 6
Posting Whiz in Training
Inny is offline Offline
293 posts
since Oct 2005
Apr 20th, 2008
0

Re: Random Topic JS

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;
Reputation Points: 10
Solved Threads: 4
Newbie Poster
HenryGR is offline Offline
24 posts
since Mar 2008
Apr 20th, 2008
0

Re: Random Topic JS

Thanks But it still goes to non existant links.
Is there some way to make it check first that the topic exist?
Reputation Points: 11
Solved Threads: 6
Posting Whiz in Training
Inny is offline Offline
293 posts
since Oct 2005
Apr 20th, 2008
0

Re: Random Topic JS

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.
Reputation Points: 10
Solved Threads: 4
Newbie Poster
HenryGR is offline Offline
24 posts
since Mar 2008
Apr 20th, 2008
0

Re: Random Topic JS

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.
Reputation Points: 11
Solved Threads: 6
Posting Whiz in Training
Inny is offline Offline
293 posts
since Oct 2005
Apr 20th, 2008
0

Re: Random Topic JS

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?
Reputation Points: 10
Solved Threads: 4
Newbie Poster
HenryGR is offline Offline
24 posts
since Mar 2008
Apr 20th, 2008
0

Re: Random Topic JS

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>
Reputation Points: 11
Solved Threads: 6
Posting Whiz in Training
Inny is offline Offline
293 posts
since Oct 2005
Apr 20th, 2008
0

Re: Random Topic JS

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. }
Reputation Points: 10
Solved Threads: 4
Newbie Poster
HenryGR is offline Offline
24 posts
since Mar 2008
Apr 20th, 2008
0

Re: Random Topic JS

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!
Reputation Points: 11
Solved Threads: 6
Posting Whiz in Training
Inny is offline Offline
293 posts
since Oct 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in JavaScript / DHTML / AJAX Forum Timeline: if statement not working
Next Thread in JavaScript / DHTML / AJAX Forum Timeline: Help required for javascript ?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC