我是新手,我正在尝试创建一个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>
答案 0 :(得分:2)
首先,该部分并不在(基本上放在标签之前,任何信息都返回给浏览器)。
其次,使用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>
答案 1 :(得分:1)
您无法将javascript函数附加到元标记。将openLink()
调用放在标记上,或者放在页面正文中。
<body onload="openLink();">