Open a random page using JavaScript - Stack Overflow

I'm newbie, I'm trying to create a redirect.html page which will send a visitor to a random s

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
Add a ment  | 

2 Answers 2

Reset to default 2

First 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

相关推荐

  • Open a random page using JavaScript - Stack Overflow

    I'm newbie, I'm trying to create a redirect.html page which will send a visitor to a random s

    4小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信