2016-07-18 34 views
0

我有一個jQuery測驗,它的工作很好,除了最後一件小事情。我希望應用能夠進入最終頁面(ThankYouPage.html)並提供一些彙總統計信息。它進入頁面,但它只顯示說:「感謝您參加這個簡短測驗......」的摘要統計信息沒有出現。你能告訴我發生了什麼事嗎?我的代碼如下。謝謝。不執行我的最後一行代碼

JS

function finalGrade() { 
    //pulls the current counter from local storage 
    var counter = localStorage.getItem('counter'); 
    var myPercent = (counter/3) * 100; 
    var myFinalPercent = myPercent.toFixed(2); 
    if (myPercent < 80) { 
     var failOrPass = "Failing"; 
      var QuestionNumber = "Quiz on Croatia."; 
      var QuizDesc = "Quiz questions on the country of Croatia."; 
      var name = localStorage.getItem('name'); 
      var email = localStorage.getItem('email'); 
      QuizFailed(email, name, QuestionNumber, QuizDesc); 
     } else { 
      var failOrPass = "Passing"; 
      //QuizPassed(); 
      } 
    document.location.replace("ThankYouPage.html"); 
    $('#summaryID').append("You scored " + counter + " out of a possible 3 for " + myFinalPercent + "%. This results in a " + failOrPass + " score."); 
} 

HTML5

<body> 

<div class="container"> 
    <h1>Thank you for taking this short quiz on the country of Croatia.</h1> 
    <p id="summaryID"></p> 
</div> 


<script src="js/init.js" type="text/javascript"></script> 
<script src="js/xapiwrapper.min.js" type="text/javascript"></script> 
</body> 

回答

0

document.location.replace( 「ThankYouPage.html」)基本導航離開,這就是爲什麼永遠達不到下一行。 ..瀏覽器已經轉移到了頁面,你告訴它加載

1

document.location.replace()調用本質上是要離開當前頁面。這裏的問題是,任何代碼仍然被執行(即你的輸出代碼)將是正被導航離開,因而不執行網頁上執行:

// Navigates to Thank You page 
document.location.replace("ThankYouPage.html"); 
// At this point you are already navigating, so this is ignored 
$('#summaryID').append('...'); 

如果你希望你的價值觀爲了出現在下一頁中,您可以考慮通過服務器端代碼,查詢字符串參數或本地存儲以某種方式傳遞它們,以便在下一頁中訪問它們。