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();">
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745508522a4630665.html
评论列表(0条)