2015-11-05 44 views
0

我正在爲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

看起來像一個stackoverflow問題 – Froggiz

+0

標記爲偏離主題。絕對屬於StackOverflow – FarhadD

回答

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> 
1

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