Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/447.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何进行html随机重定向_Javascript_Html - Fatal编程技术网

Javascript 如何进行html随机重定向

Javascript 如何进行html随机重定向,javascript,html,Javascript,Html,我将如何着手制作一个在多个(本例中为2个)网站之间随机重定向的网站 显然,下面的内容不起作用,因为我希望是meta标签;我想知道javascript代码是否是实现这一点的正确方法 以及它将如何工作 <head> <meta http-equiv="refresh" content="1;url=http://example1.com"> <script type="text/javascript"> if Math.random

我将如何着手制作一个在多个(本例中为2个)网站之间随机重定向的网站

显然,下面的内容不起作用,因为我希望是meta标签;我想知道javascript代码是否是实现这一点的正确方法 以及它将如何工作

<head>
    <meta http-equiv="refresh" content="1;url=http://example1.com">
    <script type="text/javascript">
        if Math.random() =< 0.5;
          window.location.href = "http://example1.com/"
        else;
          window.location.href = "http://example2.com"
    </script>
    <title>Page Redirection</title>
</head>

如果Math.random()=<0.5;
window.location.href=”http://example1.com/"
其他的
window.location.href=”http://example2.com"
页面重定向
您做得很对(几乎),将条件包装在
()
中并删除
,如果,它将中断

if (Math.random() =< 0.5)
    window.location.href = "http://example1.com/";
else
    window.location.href = "http://example2.com";
如果
条件始终为false,则不会发出任何警报。但是如果你看到

if(false); // If will break here.
   alert(1);
将始终像中断一样发出警报,然后执行过程将在没有if的情况下继续。

您做得很好(几乎),将条件包装在
()
中并删除
,如果,它将中断

if (Math.random() =< 0.5)
    window.location.href = "http://example1.com/";
else
    window.location.href = "http://example2.com";
如果
条件始终为false,则不会发出任何警报。但是如果你看到

if(false); // If will break here.
   alert(1);

将始终发出警报,好像中断了,然后执行继续,而没有if。

脚本标记中的javascript语法也不正确

<script type="text/javascript">
        if ( Math.random() <= 0.5 ) //proper parenthesis surrounding the condition and also < will come before =
          window.location.href = "http://example1.com/"; 
        else //semicolon removed from here
          window.location.href = "http://example2.com";
    </script>


如果(Math.random()脚本标记中的javascript语法也不正确

<script type="text/javascript">
        if ( Math.random() <= 0.5 ) //proper parenthesis surrounding the condition and also < will come before =
          window.location.href = "http://example1.com/"; 
        else //semicolon removed from here
          window.location.href = "http://example2.com";
    </script>


如果(Math.random()您可以使函数更通用一些:

  var sites = [
                        'http://www.dilbert.com',
                        'http://stackoverflow.com',
                        'http://Starwars.com'
                      ];
   var target = Math.floor(Math.random() sites.length);
    window.location = sites[target];
因此,当您的网站集增长时,您无需更改代码。您甚至可以将其打包成一个函数:

       function goRandomSite(sites) {
             var target = Math.floor(Math.random() sites.length);
             window.location = sites[target];
              }

希望这有助于

您可以使函数更通用一些:

  var sites = [
                        'http://www.dilbert.com',
                        'http://stackoverflow.com',
                        'http://Starwars.com'
                      ];
   var target = Math.floor(Math.random() sites.length);
    window.location = sites[target];
因此,当您的网站集增长时,您无需更改代码。您甚至可以将其打包成一个函数:

       function goRandomSite(sites) {
             var target = Math.floor(Math.random() sites.length);
             window.location = sites[target];
              }

希望有助于

删除
之后else@Rajesh看起来我忽略了更多我应该做的事情:)抱歉,奇怪的python习惯和普遍的愚蠢,我猜我删除@Zeludon Meta标记将不再需要了。Meta标记无论如何都不会有条件地进行刷新。删除
之后else@Rajesh看起来我忽略了更多我应该做的事情:)对不起,奇怪的python习惯和普遍的愚蠢,我猜我删除了@Zeludon Meta标签就不再需要了。Meta标记无论如何都不是有条件地进行刷新的。噢,哇,谢谢,我一直在想怎么做这样的事情。噢,哇,谢谢,我一直在想怎么做这样的事情。