我正在尝试根据条件重定向网站;如果小时数小于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,因此对您有所帮助。
这个怎么样:
<!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] 删除。
我来说两句