954,152 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Random Topic JS

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

<center><script>
//rough estimate of topics on your forum
topiccount = 2000
topicnum = Math.floor(Math.random()*topiccount)
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>")
</script>
</center>


any ideas?

Inny
Posting Whiz in Training
293 posts since Oct 2005
Reputation Points: 11
Solved Threads: 6
 

Try to change the random generation to an expression like:

topicnum = Math.round(Math.random()*(topiccount-1))+0;
HenryGR
Newbie Poster
24 posts since Mar 2008
Reputation Points: 10
Solved Threads: 4
 

Thanks But it still goes to non existant links.
Is there some way to make it check first that the topic exist?

Inny
Posting Whiz in Training
293 posts since Oct 2005
Reputation Points: 11
Solved Threads: 6
 

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:

topicnum = Math.round(Math.random()*(topiccount-1))+0;


"topiccount" must be the maximum o available topics.

HenryGR
Newbie Poster
24 posts since Mar 2008
Reputation Points: 10
Solved Threads: 4
 

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

Inny
Posting Whiz in Training
293 posts since Oct 2005
Reputation Points: 11
Solved Threads: 6
 

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?

HenryGR
Newbie Poster
24 posts since Mar 2008
Reputation Points: 10
Solved Threads: 4
 

I have Logs of Existing Topics yes..
the javascript for it (search stats) seems to be (url removed for security reasons)

<script type='text/javascript'>
 function dojump()
				
 {
				 	
if ( document.jumpmenu.val.options[document.jumpmenu.val.selectedIndex].value != '' )
				 	
{
				 		
window.location.href = 'url removed'+ '&' + document.jumpmenu.val.options[document.jumpmenu.val.selectedIndex].value;
				 	}
				 }
				 </script>
Inny
Posting Whiz in Training
293 posts since Oct 2005
Reputation Points: 11
Solved Threads: 6
 

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:

function destination() {

var urltogo = '';

while (urltogo == '')
{
selectedIndex = Math.round(Math.random()*(topiccount-1))+0;
urltogo = document.jumpmenu.val.options[document.jumpmenu.val.selectedIndex].value;
}

return urltogo;
}
HenryGR
Newbie Poster
24 posts since Mar 2008
Reputation Points: 10
Solved Threads: 4
 

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!

Inny
Posting Whiz in Training
293 posts since Oct 2005
Reputation Points: 11
Solved Threads: 6
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You