通过 Javascript/HTML 生成随机 link
Generating a random link through Javascript/HTML
我正在尝试创建一个脚本,允许我显示一个 hyperlink,将用户重定向到从四个站点中随机选择的 url。到目前为止,我已经为网站创建了一个数组,以及一个尝试生成随机 url 的函数。出于我的目的,重要的是输出 ("Click to go to a random site") 不是一个按钮,而是一个简单的(可点击的)字符串。
当 运行 代码出现引用错误时 "link is not defined (on line 18)"。我以为我在代码中用 var link = 'http://' + links[randIdx];
定义了 link,所以我不完全确定为什么会收到此错误以及如何修复它。
任何人都可以看看我的代码,看看我在哪里犯了错误,我该如何解决?
<a href="javascript:openSite()">Click to go to a random site</a>
<script>
function openSite() {
var links = [
"google.com",
"youtube.com",
"reddit.com",
"apple.com"]
openSite = function() {
// get a random number between 0 and the number of links
var randIdx = Math.random() * links.length;
// round it, so it can be used as array index
randIdx = parseInt(randIdx, 10);
// construct the link to be opened
var link = 'http://' + links[randIdx];
};
return link;
document.getElementById("link").innerHTML = openSite();
}
</script>
<a href="javascript:openSite()">Click to go to a random site</a>
<script>
var links = [
"google.com",
"youtube.com",
"reddit.com",
"apple.com"]
var openSite = function() {
// get a random number between 0 and the number of links
var randIdx = Math.random() * links.length;
// round it, so it can be used as array index
randIdx = parseInt(randIdx, 10);
// construct the link to be opened
var link = 'http://' + links[randIdx];
return link;
};
</script>
代码如下:
var links = [
"google.com",
"youtube.com",
"reddit.com",
"apple.com"]
function openSite() {
// get a random number between 0 and the number of links
var randIdx = Math.random() * links.length;
// round it, so it can be used as array index
randIdx = parseInt(randIdx, 10);
// construct the link to be opened
var link = 'http://' + links[randIdx];
return link;
};
document.getElementById("link").innerHTML = openSite();
这是一个简单的方法。
<script>
var sites = [
'http://www.google.com',
'http://www.whosebug.com',
'http://www.example.com',
'http://www.youtube.com'
];
function randomSite() {
var i = parseInt(Math.random() * sites.length);
location.href = sites[i];
}
</script>
<a href="#" onclick="randomSite();">Random</a>
我正在尝试创建一个脚本,允许我显示一个 hyperlink,将用户重定向到从四个站点中随机选择的 url。到目前为止,我已经为网站创建了一个数组,以及一个尝试生成随机 url 的函数。出于我的目的,重要的是输出 ("Click to go to a random site") 不是一个按钮,而是一个简单的(可点击的)字符串。
当 运行 代码出现引用错误时 "link is not defined (on line 18)"。我以为我在代码中用 var link = 'http://' + links[randIdx];
定义了 link,所以我不完全确定为什么会收到此错误以及如何修复它。
任何人都可以看看我的代码,看看我在哪里犯了错误,我该如何解决?
<a href="javascript:openSite()">Click to go to a random site</a>
<script>
function openSite() {
var links = [
"google.com",
"youtube.com",
"reddit.com",
"apple.com"]
openSite = function() {
// get a random number between 0 and the number of links
var randIdx = Math.random() * links.length;
// round it, so it can be used as array index
randIdx = parseInt(randIdx, 10);
// construct the link to be opened
var link = 'http://' + links[randIdx];
};
return link;
document.getElementById("link").innerHTML = openSite();
}
</script>
<a href="javascript:openSite()">Click to go to a random site</a>
<script>
var links = [
"google.com",
"youtube.com",
"reddit.com",
"apple.com"]
var openSite = function() {
// get a random number between 0 and the number of links
var randIdx = Math.random() * links.length;
// round it, so it can be used as array index
randIdx = parseInt(randIdx, 10);
// construct the link to be opened
var link = 'http://' + links[randIdx];
return link;
};
</script>
代码如下:
var links = [
"google.com",
"youtube.com",
"reddit.com",
"apple.com"]
function openSite() {
// get a random number between 0 and the number of links
var randIdx = Math.random() * links.length;
// round it, so it can be used as array index
randIdx = parseInt(randIdx, 10);
// construct the link to be opened
var link = 'http://' + links[randIdx];
return link;
};
document.getElementById("link").innerHTML = openSite();
这是一个简单的方法。
<script>
var sites = [
'http://www.google.com',
'http://www.whosebug.com',
'http://www.example.com',
'http://www.youtube.com'
];
function randomSite() {
var i = parseInt(Math.random() * sites.length);
location.href = sites[i];
}
</script>
<a href="#" onclick="randomSite();">Random</a>