2015-04-08 60 views
-1

我有一個JS在任何新通知到達時播放通知聲音。它的工作原理完全正常,當一個新通知到來時,它應該播放聲音,它確實發揮。.html()在頁面更改或刷新時不起作用

notification我在說的只是一個integer,它是通過ajax調用從查詢返回的。我通過腳本將此integer設置爲我的<asp:label.../>

的問題是:我已經寫上MasterPage腳本。因此,每當我打開新頁面或刷新同一個頁面時,<asp:Label.../>被清除,我通過腳本使用.html(value)來設置,導致腳本在每次頁面刷新或其他頁面加載時再次運行聲音。

問題可能是,該值不是持續

我希望值應該被設置爲標籤的html,並且其值也應該在所有頁面上保留。我應該怎麼做這個persistence

我的腳本是:通過類選擇

<script type="text/javascript"> 
      myFunction(); 

      function myFunction() { 
       $.get("AjaxServicesNoty.aspx", function (data) { 
        var recievedCount = parseInt(data); 
        alert(recievedCount); 
        var existingCount = $(".lblEventCount").text(); 

        if (existingCount == "") { 
         existingCount = 0; 
         alert(existingCount); 
        } 
        else { 
         existingCount = parseInt($(".lblEventCount").text()); 
         alert(existingCount); 
        } 

        //     if (existingCount == "" && recievedCount != 0) { 
        //      $(".lblEventCount").html(recievedCount); 
        //      $(".lblAcceptedCount").html(recievedCount); 
        //      var sound = new Audio("Sound/notificationSound.wav"); 
        //      sound.play(); 
        //     } 

        if ((parseInt(recievedCount) > parseInt(existingCount)) && existingCount == 0) { 
         $(".lblEventCount").html(recievedCount); 
         $(".lblAcceptedCount").html(recievedCount); 
         var sound = new Audio("Sound/notificationSound.wav"); 
         sound.play(); 
        } 
        else { 
         $(".lblEventCount").html(existingCount); 
         $(".lblAcceptedCount").html(existingCount); 
        } 
       }); 
      } 
      setInterval(myFunction, 5000); 
     </script> 
+1

我不明白,很好,但在客戶端,如果你刷新asp.net頁面或從服務器重新加載頁面,這些值將不會持久。你嘗試過使用cookie嗎? – Serendipity

+0

再次我不能訪問來自JS的會話和cookie值?我可以嗎 ? –

+0

我剛剛看過,我可以從JS訪問cookies!無論如何,我會找到解決辦法!謝謝! –

回答

0

使用DIV ID而不是

$("#divID").html(existingCount); 
+0

的值是否會持久? –

+0

不需要改變價值。只需嘗試使用ID選擇。我不確定只是試試。 – Munna

+1

它不會工作!它會破壞CSS!! Anews我解決了它! –

相關問題