使用window.location.replace(“ xx”)重定向页面;根据条件

埃鲁特夫

我正在尝试根据条件重定向网站;如果小时数小于12,则重定向到xxx.com,否则重定向到yyy.com,即合并以下两段代码:

var hour = new Date().getHours();
var greeting;
if (hour < 12) {
    greeting = "Good day";
} else {
    greeting = "Good evening";
}
document.getElementById("demo").innerHTML = greeting;
<!DOCTYPE html>
<html>
  <body>
    <p id="demo"></p>
  </body>
</html>

window.location.replace("http://xx.com");

我刚刚开始学习js,因此对您有所帮助。

拉杜·迪亚(Radu Diță)

这个怎么样:

<!DOCTYPE html>
<html>
<body>

<p id="demo"></p>

<script>
 window.onload = function() {
  var hour = new Date().getHours();
  if (hour < 12) {
    window.location.replace("http://xxx.example.com");
   } else {
    window.location.replace("http://yyy.example.com");
   }
  }
 </script>

 </body>
</html>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

window.location.replace(); 不是在单击按钮时重定向页面

在window.location.replace中使用参数

使用$ window或$ location在AngularJS中重定向

如果条件为真,为什么Javascript window.location无法重定向页面?

Javascript window.location.replace只是重新加载了此页面

window.location.href不重定向html页面

如何在jquery或javascript中使用window.location.replace

Javascript 使用 .hash 重定向 window.location.url

使用document.location.replace重定向后如何记住上一个URL

window.location.replace-POST版本?

window.location.replace奇怪的行为

反应版本的 window.location.replace()?

window.location.href-帮助重定向

如何不使用window.location重定向到另一个HTML页面?

如果重新加载同一页面,则window.location.replace无法正常工作

JavaScript window.location无法重定向到其他页面

window.location不会在空白页面上重定向吗?

如何使用window.location.href.replace切换href网址中的语言?

Phonegap-在我使用window.location.replace离开索引范围后处理推送通知

我在使用window.location.replace吗?(我也是在写命令吗?)

使用window.location重定向时,ngStorage无法正常工作

WordPress:从首页调用时,使用window.location的Javascript重定向问题

后台页面可以使用window.location吗

使用window.location定位特定的html页面

window.location =和window.location.replace()有什么区别?

window.location.assign()和window.location.replace()之间的区别

是否可以捕获window.location.replace事件?

将变量放置在window.location.replace()内

关于window.location.href.replace的各种可能性