2014-09-21 90 views
-2

我正在嘗試製作一個頁面,在用戶輸入頁面10秒後彈出確認框。它說:「你想離開這個頁面嗎?」如果用戶點擊確定,它會將用戶傳送給Google。如果用戶點擊取消,會彈出一個提示,提示「確定,您可以留下。」這只是一個練習。確認聲明w/if else

HTML:

<html> 
<head> 
</head> 
<body> 
<h1>Something.</h1> 
<ul> 
<li>Info...</li> 
<li>More...</li> 
<li>Yet More...</li> 
</ul> 
<script type="text/javascript" src="confirm.js"></script> 
</body> 
</html> 

的JavaScript:

function stayonPage(){ 
var is_sure = window.confirm("Do you want to leave this page?"); 
} 
var waittime = window.setTimeout("stayonPage()",10000); 

if(is_sure=false;) 
{window.alert("OK. You can stay.");} 
else 
{window.location="http://www.google.com";} 

到目前爲止,該網頁顯示一秒鐘,然後去谷歌沒有確認框中顯示。

+0

井的測試答案

  • 你分配is_sure代替,您在詢問之前等待,但不要在檢查答案之前等待和重定向!把你的重定向代碼放在'stayonPage'函數中。 – Bergi 2014-09-21 11:22:38

  • 回答

    1

    這是因爲腳本繼續執行。

    setTimeout將在10秒後彈出問題,因爲您在十秒後將此設置爲回調函數。 但 - 腳本在此期間繼續,發現is_sure變量未定義,所以去else聲明。

    您可以將if語句放入stayonPage函數中,該函數在10秒後執行時會執行該函數。

    0

    您需要將您的重定向的if/else塊stayonPage功能,以及使用比較 - 運營商,而不是分配的:

    function stayonPage(){ 
        var is_sure = window.confirm("Do you want to leave this page?"); 
        if(is_sure == false) // here you need to use == instead of = and no semi-colon 
         {window.alert("OK. You can stay.");} 
        else 
         {window.location="http://www.google.com";} 
    } 
    var waittime = window.setTimeout("stayonPage()",10000); 
    

    整個事情的優化版本是這樣的:

    setTimeout(function(){ 
        if (!confirm("Do you want to leave this page?")) 
         alert("OK. You can stay."); 
        else 
         window.location="http://www.google.com"; 
    }, 10000); 
    

    見這裏:http://jsfiddle.net/un52w63c/(注意,重定向不上的jsfiddle工作)

    0

    一分夫婦的問題:

    • 您沒有要等待比較它

    function stayonPage(){ 
     
    var is_sure = window.confirm("Do you want to leave this page?"); 
     
    // move it inside the function 
     
    if(is_sure==false) // compare, don't assign 
     
    {window.alert("OK. You can stay.");} 
     
    else 
     
    {window.location="http://www.google.com";} 
     
    } 
     
    setTimeout(stayonPage, 10000); 
     
    // don't need to keep a reference, and don't need to use window 
     
    // we can just reference the function instead of using implicit eval

    0
    function stayonPage(){ 
        var is_sure = window.confirm("Do you want to leave this page?"); 
    
    /* is_sure is scoped inside this function it is not a global and 
    can not be used from a different global function */ 
    
    /* is_sure=false sets is_sure to false and always returns false and 
    because it is already a true or false so no condition operation is required */ 
    
        if(is_sure) 
        {window.location="http://www.google.com"; 
        } 
        else 
        {window.alert("OK. You can stay."); 
        } 
    } 
    
    
    
    var waittime = window.setTimeout("stayonPage()",10000); 
    /* 
    I assume this will be replace by something more logical, 
    It will ask if you are sure you wanted to leave without the user 
    indicating he wanted to leave. 
    */