创建随机重定向按钮

时间:2018-03-05 01:56:02

标签: javascript html url redirect

我有以下代码,我想使用一个大的中心对齐按钮将访问者的网站随机重定向到一个网址之一:

<html>
<head>
<script type="text/javascript">
<!--
// Create an array of the links to choose from:
var links = new Array();
links[0] = "http://www.google.com/";
links[1] = "http://www.bing.com/";
links[2] = "http://www.yahoo.com/";
links[3] = "http://www.apple.com/";

function openLink() {
  // Chooses a random link:
  var i = Math.floor(Math.random() * links.length);
  // Directs the browser to the chosen target:
  parent.location = links[i];
  return false;
}
//-->
</script>
</head>
<body onload="openLink();">
</body>
</html>

我应该如何调整上述代码来实现这一目标?谢谢你的帮助。

2 个答案:

答案 0 :(得分:0)

试试这个

mysql -p mysql
修改:代码段不能正常运行。尝试将其置于测试环境中。我打开了一个空白标签,并使用Inspect Element的控制台进行测试,结果正常。

答案 1 :(得分:0)

尝试使用此代码

    <html>
<head>
<script type="text/javascript">
<!--
// Create an array of the links to choose from:
var links = new Array();
links[0] = "http://www.google.com/";
links[1] = "http://www.bing.com/";
links[2] = "http://www.yahoo.com/";
links[3] = "http://www.apple.com/";

function openLink() {
  // Chooses a random link:
  var i = Math.floor(Math.random() * links.length);
  // Directs the browser to the chosen target:
  parent.location = links[i];
  return false;
}
//-->
</script>
<style>
.btn {
    height:50px;
    width:200px;
    background:#125288;
    color:#fff;
    font-size:18px;
}
</style>
</head>
<body>
</body>
<center>
<button onclick="openLink();" class='btn'>Try to Click</button>
</center>
</html>