我是新手,我正在尝试创建一个redirect.html页面,该页面将在访问者打开重定向页面后立即将其发送到随机站点。请帮忙编辑以下代码,我认为问题出在这一行:
"echo "<meta http-equiv='refresh' content=0;URL="openLink();">"
<小时/>
<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>
echo "<meta http-equiv='refresh' content=0;URL="openLink();">
</body>
</html>
最佳答案
首先,该部分并不位于(基本上放置在任何信息返回到浏览器之前的标签中)。
其次,使用 META 标记并不是当今最好的格式,但如果您必须使用它:您可以使用 Javascript 来构建 META 标记,使用如下内容:
<script type="text/javascript">
var urls = new Array("http://www.google.com/", "http://www.yahoo.com/");
function redirect()
{
window.location = urls[Math.floor(urls.length*Math.random())];
}
var temp = setInterval("redirect()", 3000);
</script>
但是,根据您的代码,从 META 标记中删除 openLink() 调用并将其放在 onload 上:
<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>
关于javascript - 使用 JavaScript 打开随机页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24514214/