Question

I’m writing code that generates a random link to be prepended to another other link from a text field, like this:

http://{generated link}/{link entered in a text field}

I tried this, but is not working for me; I do not know the error.

<SCRIPT Language="Javascript">
function Randomlink()
{
    Url = new Array;
    Url[0] = "http://www.google.com";
    Url[1] = "http://www.youtube.com";
    Url[2] = "http://www.facebook.com";
    Url[3] = "http://www.yahoo.com";

    Chooselink = Math.round(Math.random() * (Url.length+1));

}
</SCRIPT>

<center>
<input id="input" name="url" onfocus="this.value='' type="text" value="Type url here.." />
<input  onclick="window.open(###randomize link here###+ window.document.getElementById('input').value.replace(/^https?:\/\//,''))" style="font-family: Arial, sans-serif; font-size: 0.9em; margin: 2px 0; padding: 4px; width: 100px;" type="button" value="Go"/>
</center>
Was it helpful?

Solution

I modified your code little bit and it worked as expected:

JS change at here: return Url...

Modified code

<SCRIPT Language="Javascript">
function Randomlink()
{
    Url = new Array;
    Url[0] = "http://www.google.com";
    Url[1] = "http://www.youtube.com";
    Url[2] = "http://www.facebook.com";
    Url[3] = "http://www.yahoo.com";

    return Url[Math.floor(Math.random() * Url.length)];

}
</SCRIPT>

HTML change at here: window.open Randomlink...

Modified code

<center>
<input id="input" name="url" onfocus="this.value=''" type="text" value="Type url here.." />
 <input  onclick="window.open(Randomlink()+'/'+ window.document.getElementById('input').value.replace(/^https?:\/\//,''))" style="font-family: Arial, sans-serif; font-size: 0.9em; margin: 2px 0; padding: 4px; width: 100px;" type="button" value="Go"/>
</center>

Cheers

OTHER TIPS

If you put my comment together, it looks something like this:

<head>
    ⋮

    <!-- external stylesheet, because it’s good practice -->
    <link rel="stylesheet" type="text/css" href="stylesheet.css" />
</head>

<body>
    <form id="search">
        <!-- placeholder, because it’s semantically correct,
             good-looking, and not annoying -->
        <input type="url" name="q" placeholder="Type a URL here" />

        <!-- submit button, because it’s semantically correct,
             and usable through Enter for free -->
        <input type="submit" id="go" value="Go" />
    </form>

    <!-- external script, because it’s good practice -->
    <script type="text/javascript" src="script.js"></script>
</body>
#search {
    text-align: center;
}

#go {
    font-family: Arial, sans-serif;
    font-size: 0.9em;
    margin: 2px 0;
    padding: 4px;
    width: 100px;
}
"use strict";

// A more generic function to pick a random element from an array (or array-like)
function randomChoice(list) {
    // | 0 truncates the result to a 32-bit integer
    return list[Math.random() * list.length | 0];
}

// Set the action for the search form; this isn’t quite correct yet
var search = document.getElementById("search");

search.action = randomChoice([
    "https://www.google.com/",
    "https://www.youtube.com/",
    "https://www.facebook.com/",
    "https://www.yahoo.com/"
]);

That may not be perfectly what you’re after, but it’s a much better place to start.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top