Javascript 页面重定向不工作
有人知道为什么我的重定向不起作用吗?我浏览过论坛,看不出我做错了什么。请注意if语句的条件为false。重定向应该发生。。。为什么不是呢Javascript 页面重定向不工作,javascript,html,redirect,iframe,Javascript,Html,Redirect,Iframe,有人知道为什么我的重定向不起作用吗?我浏览过论坛,看不出我做错了什么。请注意if语句的条件为false。重定向应该发生。。。为什么不是呢 <!DOCTYPE html> <html> <body> <p>Click the button to get a time-based greeting.</p> <button onclick="myFunction()">Try it</button>
<!DOCTYPE html>
<html>
<body>
<p>Click the button to get a time-based greeting.</p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
<script>
function myFunction() {
var x="";
if (new Date("2014-03-04 21:00:00") < new Date()) {
x="GOOD";
} else {
window.location = "http://www.google.com";
}
document.getElementById("demo").innerHTML=x;
}
</script>
</body>
</html>
单击按钮以获取基于时间的问候语
试试看
函数myFunction(){
var x=“”;
如果(新日期(“2014-03-04 21:00:00”)<新日期()){
x=“好”;
}否则{
window.location=”http://www.google.com";
}
document.getElementById(“demo”).innerHTML=x;
}
应该是:
window.location.href = "http://www.google.com";
好吧,你的代码在大多数浏览器中都能工作。如果要在旧版本的internet explorer中测试此功能,可能需要指定标准。试着像这样运行它:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<body>
<p>Click the button to get a time-based greeting.</p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
<script type="text/javascript">
<!--
function myFunction() {
var x="";
if (new Date("2014-03-04 21:00:00") < new Date()) {
x="GOOD";
} else {
window.location.href = "http://www.google.com";
}
document.getElementById("demo").innerHTML=x;
}
// -->
</script>
</body>
</html>
单击按钮以获取基于时间的问候语
试试看
我已经更新了doctype并指定了脚本类型。另外,请使用window.location.href而不是window.location,这将避免用户尝试返回上一页时可能遇到的无限循环。我无法在Firefox或Chrome上使用它。按钮消失了,但实际上什么也没发生。