Javascript 重定向网站日期

标签 javascript html

伙计们。我目前正在制作一个网站,但我想让我的网站自动重定向到星期几,而不需要按链接。示例:如果是星期一,网站将重定向到 monday.html。

我的代码:

<!DOCTYPE -be sure to use one of these! 
<html> 
<head> 
<title></title> 
<script type="text/javascript"> 
window.onload = function(){chgDailyImg();} 
function chgDailyImg() 
{ 
var imagearray = new Array(); 
imagearray[0] = "sundaypic.png"; 
imagearray[1] = "mondaypic.png"; 
imagearray[2] = "tuesdaypic.png"; 
imagearray[3] = "wednesdaypic.png"; 
imagearray[4] = "assets/images/thursdaypic.png"; 
imagearray[5] = "fridaypic.png"; 
imagearray[6] = "saturdaypic.png";

var linkarray = new Array(); 
linkarray[0] = "sondag.html"; 
linkarray[1] = "mandag.html"; 
linkarray[2] = "tirsdag.html"; 
linkarray[3] = "onsdag.html"; 
linkarray[4] = "torsdag.html"; 
linkarray[5] = "fredag.html"; 
linkarray[6] = "lordag.html";

var textarray = new Array(); 
textarray[0] = "This is sunday's text - click to go to sunday's page"; 
textarray[1] = "This is monday's text - click to go to monday's page"; 
textarray[2] = "This is tuesday's text - click to go to tuesday's page"; 
textarray[3] = "This is wednesday's text - click to go to wednesday's page"; 
textarray[4] = "Torsdag"; 
textarray[5] = "This is friday's text - click to go to friday's page"; 
textarray[6] = "This is saturday's text - click to go to saturday's page";

var d = new Date(); /*** create a date object for use ***/ 
var i = d.getDay(); /*** use the date object to get the day of the week - this will be a number from 0 to 6 - sunday=0, saturday=6 -it's the way counting works in javascript it starts at 0 like in the arrays ***/ 
document.getElementById("dailyImg").src = imagearray[i]; 
document.getElementById("dailyLink").href = linkarray[i]; 
document.getElementById("dailyLink").innerHTML = textarray[i]; 
} 
</script> 
</head> 
<body> 
<div> 
<a href="sundaypage.html" id="dailyLink">This is sunday's text - click to go to sunday's page</a> 
<img src="sundaypic.jpg" alt="daily pic" title="daily pic" width="300" height="100" id="dailyImg" /> 
</div> 
</body> 
</html>

最佳答案

您所需要做的就是设置 window.location.href linkarray[i] 而不是将其写入页面:

<!DOCTYPE html>
<html>

<head>
  <title></title>
  <script type="text/javascript">
    window.onload = function() {
      chgDailyImg();
    }

    function chgDailyImg() {
      var imagearray = new Array();
      imagearray[0] = "sundaypic.png";
      imagearray[1] = "mondaypic.png";
      imagearray[2] = "tuesdaypic.png";
      imagearray[3] = "wednesdaypic.png";
      imagearray[4] = "assets/images/thursdaypic.png";
      imagearray[5] = "fridaypic.png";
      imagearray[6] = "saturdaypic.png";

      var linkarray = new Array();
      linkarray[0] = "sondag.html";
      linkarray[1] = "mandag.html";
      linkarray[2] = "tirsdag.html";
      linkarray[3] = "onsdag.html";
      linkarray[4] = "torsdag.html";
      linkarray[5] = "fredag.html";
      linkarray[6] = "lordag.html";

      var textarray = new Array();
      textarray[0] = "This is sunday's text - click to go to sunday's page";
      textarray[1] = "This is monday's text - click to go to monday's page";
      textarray[2] = "This is tuesday's text - click to go to tuesday's page";
      textarray[3] = "This is wednesday's text - click to go to wednesday's page";
      textarray[4] = "Torsdag";
      textarray[5] = "This is friday's text - click to go to friday's page";
      textarray[6] = "This is saturday's text - click to go to saturday's page";

      var d = new Date(); /*** create a date object for use ***/
      var i = d.getDay(); /*** use the date object to get the day of the week - this will be a number from 0 to 6 - sunday=0, saturday=6 -it's the way counting works in javascript it starts at 0 like in the arrays ***/

      // Automatically redirect to the day's page
      window.location.href = linkarray[i];
    }
  </script>
</head>

</html>

希望这有帮助! :)

关于Javascript 重定向网站日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45870524/

相关文章:

javascript - 如何使用 jQuery 获取客户端 IP 地址

javascript - 有没有一种方法可以在Electron中为子窗口设置属性?

javascript - 过滤路径并附加文本

javascript - “私有(private)”细胞

javascript - 嵌入式对象后面的 jquery 组合框(仅限 IE)

html - 新窗口或新标签——如何决定?

javascript - 使用 javascript 将 HTML 输入标签转换为 Dojo TextBox

html - CSS3 动画中 Firefox 的背景图像

javascript - 加载浏览器时在后台持续运行 chrome 扩展

javascript - 如何在 gatsby 中添加外部 Javascript?