3

I have coded a simple chat application in php. I want to know that how to put a blinking title on title bar to notify a user that he received a new message if he is switched to other tab. My codes: Javascript:

//messager fetcher

function reload_content() {



var xmlhttp;

if (window.XMLHttpRequest)
  {
  xmlhttp=new XMLHttpRequest();
  }

xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("chatBox").innerHTML=xmlhttp.responseText;
    }
  }

   xmlhttp.open("POST","messages.php",true);
xmlhttp.send();
    scrollToBottom();
}
window.setInterval(reload_content, 1000);

and code for messages.php

<?php


  //database connection starts
$con = mysql_connect("******","******","******");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("chat", $con);
//database connection ends

$display_message = mysql_query("SELECT * FROM `messages` WHERE `myname` = '$_SESSION[name]' or `friend` = '$_SESSION[name]'");
 if($display_message === FALSE) {
    die(mysql_error()); // TO DO: better error handling
}


     while($row_display=mysql_fetch_array($display_message))
        {


    $display=$row_display['msg'];
}
echo $display;
?>
Webmasterpsb
  • 71
  • 1
  • 6

3 Answers3

1

Modify document.title (by assigning new values to it) repeatedly using setInterval.

You may wish to determine what the new value should be by taking the initial value and running regular expressions over it.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
1

To detect if the user has switched to other tab, use this trick:

var isActive;

window.onfocus = function () { 
  isActive = true; 
}; 

window.onblur = function () { 
  isActive = false; 
}; 

// test
setInterval(function () { 
  console.log(window.isActive ? 'active' : 'inactive'); 
}, 1000);

Source : https://stackoverflow.com/a/1760283/652669

Then just update the title with:

document.title = 'message';
Community
  • 1
  • 1
GG.
  • 21,083
  • 14
  • 84
  • 130
0

Set the windows title in your javascript, there is an example here:

http://www.javascripter.net/faq/windowti.htm

Arjun Sol
  • 731
  • 5
  • 18