Javascript警报和重定向脚本意外行为

问题描述:

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

他们可以选择单击确认进入站点(我将调用此警报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> 
+0

看起来像一个*问题 – Froggiz

+0

标记为偏离主题。绝对属于* – FarhadD

您忘记了{}如果你不把{} 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> 

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"; 
 
}