PDA

View Full Version : What can I do to make this script work??????



IanMarlowe
07-19-2005, 12:47 PM
<form name="random_link"> //this is wrong I think.
<input type=text size=32 width=32 name="textbox1" style="text-align=center"> //is that all right?
</form>
<script type="text/javascript">
random_link = new Object(); //is this correct? new Object()?
var r=Math.random()
if (r>5)
{
document.write("<a href='home.html'>Home</a>") //example
}
else
{
document.write("<a href='index.html'>Index</a>") //example
}
document.random_link.textbox1.value = //this is where I'm stuck
</script>


</td>
<td colspan=2 height=24></td>
</tr>
<tr valign="top">
<td colspan=4 height=227></td>
</tr>
</table> //what does all that mean?

:(
Do I have aannyytthhiinngg right in there?

jscheuer1
07-19-2005, 02:51 PM
Ungh.

Math.random()

is never greater than 5. It is always >=0 but, <1, so to get what you are looking for, by way of a test try:

r>.5

Never use javascript style comments in html, they appear as regular text.

You don't need a form or text input for this script. You don't need to create a new Object() either. This is all you need, it goes in the body of the page, where you want the random link to appear:

<script type="text/javascript">
var r=Math.random()
if (r>.5)
document.write("<a href='home.html'>Home</a>")
else
document.write("<a href='index.html'>Index</a>")
</script>With only two links to choose from, often you will get the same one over and over. You actually have a good idea that is discernible in this script.

IanMarlowe
07-20-2005, 04:57 AM
but i would like it to be in a box.

--if you have, time is there a way to make it so it's a random html file?

like if (r<1) <html src="blah.html">?

jscheuer1
07-20-2005, 09:19 AM
What you are asking for is not precisely possible. You cannot put an html file inside of a text input (well, you can but, the result would not be pretty). So, the question becomes what effect are you trying to achieve? Forget about technical terms for the moment. What do you want the page to look like and what (if anything) do you want to have happen when it loads? What to you want to have happen as the user interacts with it?

Just for kicks I messed around with this idea a bit sometime yesterday. This is not meant to be a solution for you, just something to check out and play with if you like:
<html>
<head>
<title>Random Link Rotator</title>
<script type="text/javascript">
function randLink(){
var blip, rlink, r=Math.random()*100
if (r>=25&&r<50)
rlink='<a href="home.html">Home</a>'
else if (r>=50&&r<75)
rlink='<a href="downloads.html">Downloads</a>'
else if (r>=75)
rlink='<a href="sitemap.html">Site Map</a>'
else
rlink='<a href="index.html">Index</a>'
crossObj=document.all? document.all.rdiv : document.getElementById('rdiv')
blip=crossObj.innerHTML.toString()
blip=blip.substr(blip.lastIndexOf('"'))
if (rlink.toLowerCase().substr(rlink.lastIndexOf('"'))!==blip.toLowerCase())
crossObj.innerHTML=rlink
else
randLink()
}
</script>
</head>
<body>
<div id="rdiv"><a href="index.html">Index</a></div><br>
<span>Click Below to Generate a Random Link</span><br>
<input type="button" value="Random Link" onclick="randLink()">
</body>
</html>

IanMarlowe
07-20-2005, 11:29 AM
that is exactly what I neeed. Instead of putting html tags and javascript tags, I could just save it as a .js, and:


<form input="<script type="text/javascript" src="random_link_rotator.js">">
</form>