如何检查表中的新内容,然后更新浏览器选项卡标题?

时间:2012-02-23 16:57:58

标签: javascript jquery html

我正在研究在表格中出现新消息时闪存浏览器选项卡的方法。我的标签部分闪烁工作,我唯一的问题是,当收到一条消息时,我似乎无法让它闪现(这是我锻炼的全部意义:) :)

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非常生疏,如果我犯了任何愚蠢的错误,请原谅我!

谢谢,

1 个答案:

答案 0 :(得分:0)

你在函数通知()中有几个错误,请参阅我的评论:

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