Javascript Alert&重定向脚本意外行为

时间:2015-11-05 22:45:15

标签: javascript

我今天正在为tumblr上的博客制作一个小小的警报脚本,但我注意到我的脚本无论他们选择什么作为决定都会重定向。

他们可以选择单击“确认”进入该站点(我将其称为此警报X),或者选择“取消”以弹出其他警告,告诉他们将重定向到他们的仪表板(我将其称为警报Y)。问题是重定向只能在警报Y中单击“确定”后才会发生。但是,在警报X中单击“确认”后,它也会重定向。任何人都可以找出我错在哪里?我是新手/ noob @ Javascript;还在学习,取得进步!

<script type="text/javascript">
var x=window.confirm("WARNING! This blog contains depressing content and possibly vivid imagery! Do you really want to continue? Click OK to continue; Click Cancel to leave now. ")
if (x)
    window.alert('You can now close this box and browse the blog!')
else
    var c=window.alert("It >Might< be better if you leave the blog now."); 
    window.location = "http://www.tumblr.com/dashboard"
</script>

2 个答案:

答案 0 :(得分:1)

&#13;
&#13;
var x = window.confirm("WARNING! This blog contains depressing content and possibly vivid imagery! Do you really want to continue? Click OK to continue; Click Cancel to leave now. ")
  if (x)
    window.alert('You can now close this box and browse the blog!')
  else {
    var c = window.alert("It >Might< be better if you leave the blog now.");
    window.location = "http://www.tumblr.com/dashboard";
 }
&#13;
&#13;
&#13;

答案 1 :(得分:0)

你忘了{}如果你不放{} javascript条件只允许一个语句,所以windows.location不在条件中并且总是被触发

<script type="text/javascript">
var x=window.confirm("WARNING! This blog contains depressing content and possibly vivid imagery! Do you really want to continue? Click OK to continue; Click Cancel to leave now. ")
if (x)
    {window.alert('You can now close this box and browse the blog!');}
else{
    var c=window.alert("It >Might< be better if you leave the blog now."); 
    window.location = "http://www.tumblr.com/dashboard";
}
</script>