簡體   English   中英

如何檢查表中的新內容,然后更新瀏覽器選項卡標題?

[英]How to check table for new content, and then update browser tab title?

當表格中出現新消息時,我正在設法刷新瀏覽器標簽。 我的選項卡部分正在閃爍,我唯一的問題是,收到消息后,似乎無法使其閃爍(這是我練習的全部要點:))

newMessage()函數工作正常,我似乎無法使notification()函數正常工作。

我的代碼如下:

function newMessage() 
     {
      var oldTitle = "Your Page";
      var msg = "New Message";

      var timeout = setInterval(function() 
      { 
        document.title = document.title == msg ? '' : msg;
      }, 1000);

      window.onmousemove = function() {
          clearInterval(timeout);
          document.title = oldTitle;
          window.onmousemove = null;
      };
     }

function notification()
     {
       var index = 2;

       var content = document.getElementById('refreshMessages').childNodes[index];

        var content = document.getElementById('refreshMessages').getElementByTagName("tr")[1];

        var knownContent = content.toString();

        updater.start();

        updater2.start();

        var newContent = document.getElementById('refreshMessages').childNodes[index];

        var newContent = document.getElementById('refreshMessages').getElementByTagName("tr")[1];

        if(knownContent != newContent.toString())
        {
           newMessage();
           knownContent = newContent;
        }
        else if(knownContent = newContent.toString())
          {
            alert("No need to flash title.");
          }
     }
     notification();

notification()函數中,我試圖通過比較表中適當單元格處的字符串來調用newMessage()函數。

我將alert()放到else中,只是為了查看它是否會被調用,但不會發生。 但是,執行update.start()update2.start()過程如我所見,該消息出現在表中。

我會更高興使用JavaScript,但我也對jQuery開放。

我的JavaScript非常生銹,如果我犯了任何愚蠢的錯誤,請原諒!

謝謝,

卡盤

您在函數notification()中有幾個錯誤,請參閱我的評論:

function notification()
{
    var index = 2;

    //Why are you assigning value to "content" for twice?
    var content = document.getElementById('refreshMessages').childNodes[index];
    /*
    * function getElementByTagName is undefined, should be getElementsByTagName, 
    *  's' is missing. And [1] means the second one not the first one, make sure
    *  that's exactly what you want.
    */
    var content = document.getElementById('refreshMessages').getElementByTagName("tr")[1];

    /*
    * content is a tr dom object, content.toString() is something like "[object]".
    * If you want to get content inside a cell, you should use cell.innerHTML.
    * e.g. A table: 
    * <table id="refreshMessages">
    *     <tr><td>Hello world</td></tr>
    * </table>
    * var table = document.getElementById('refreshMessages');
    * var firstTr = table.getElementsByTagName("tr")[0];
    * var firstTd = firstTr.getElementsByTagName("td")[0];
    * alert(firstTd.innerHTML);      //alerts "Hello world"
    */
    var knownContent = content.toString();

    //I doubt these functions really get invoked cuz there's javascript error above.
    updater.start();

    updater2.start();

    //assigning twice, "getElementByTagName" is missing "s"
    var newContent = document.getElementById('refreshMessages').childNodes[index];
    var newContent = document.getElementById('refreshMessages').getElementByTagName("tr")[1];

    //Remove toString(), use innerHTML i metioned above.
    if(knownContent != newContent.toString())
    {
       newMessage();
       knownContent = newContent;
    }
    //You miss an "=" here, to judge a equals b, you should use "=="
    else if(knownContent = newContent.toString())
    {
        alert("No need to flash title.");
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM