admin管理员组文章数量:1429786
I'm newbie, I'm trying to create a redirect.html page which will send a visitor to a random site as soon as he open the redirect page. Please help to edit the following code, I think the issue is in this line:
"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] = "/";
links[1] = "/";
links[2] = "/";
links[3] = "/";
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>
I'm newbie, I'm trying to create a redirect.html page which will send a visitor to a random site as soon as he open the redirect page. Please help to edit the following code, I think the issue is in this line:
"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./";
links[1] = "http://www.bing./";
links[2] = "http://www.yahoo./";
links[3] = "http://www.apple./";
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>
Share
Improve this question
asked Jul 1, 2014 at 15:36
Juninho10Juninho10
231 silver badge8 bronze badges
2
- 1 why don't you do it with a server-side redirect? Are you aware of them? – Vlas Bashynskyi Commented Jul 1, 2014 at 15:38
- 1 Don't mix up back end and front end parts of the website. – VisioN Commented Jul 1, 2014 at 15:38
2 Answers
Reset to default 2First of all, the section and not in the (basically placed in a tag before any information is returned to the browser).
Secondarily, using the META tag isn't the best format to use these days but if you have to use it : you can use Javascript to build a META tag, using something like :
<script type="text/javascript">
var urls = new Array("http://www.google./", "http://www.yahoo./");
function redirect()
{
window.location = urls[Math.floor(urls.length*Math.random())];
}
var temp = setInterval("redirect()", 3000);
</script>
But, as per your code, remove the openLink() call from the META tag and place it on the 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./";
links[1] = "http://www.bing./";
links[2] = "http://www.yahoo./";
links[3] = "http://www.apple./";
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>
You can't attach javascript functions to the meta tag. Put your openLink()
call on the tag, alternatively within the body of the page.
<body onload="openLink();">
本文标签: Open a random page using JavaScriptStack Overflow
版权声明:本文标题:Open a random page using JavaScript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745508522a2661360.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论