web notifications in <title> from chatbox

こ雲淡風輕ζ 提交于 2019-12-13 15:18:43

问题


I'm trying to put web notifications in the title/tab screen of my webpage. So whenever someone new says something and you're not on the tab, the will give notifications that you have a new message.

Is there any simple way to go about this? Here's a live link of the chat would i have to tamper with my post.php? or would

<?
session_start();
 if(isset($_SESSION['name'])){
$text = $_POST['text']; 
$fp = fopen("log.html", 'a');
fwrite($fp, "<div class='msgln'> C:\Users\<b>".$_SESSION['name']."></b> ".stripslashes(htmlspecialchars($text))."<br></div>");
fclose($fp);
}
?>

or would i tamper with the input ?

$(document).ready(function(){
//If user submits the form
$("#submitmsg").click(function(){   
    var clientmsg = $("#usermsg").val();
    $.post("post.php", {text: clientmsg});              
    $("#usermsg").attr("value", "");
    return false;
});

thanks in advance for any help !


回答1:


I'd suggest you to have a look at the JavaScript function setInterval(). Make this function check for updates every x ms, if it finds any updates; edit the page-title.

Something like this should do the trick, tweak to get preferred result:

// This function will run every ~1s
setInterval(function() {

   // Get the new data
   $.post('post.php', function(data) {

      // Handle the data
      document.title = data;

   });


}, 1000);

You should definitely add some error checking on the PHP-script (check if variables are set: isset($_SESSION['value'])), possibly at the javascript too.




回答2:


see "document.title" in javascript.

var xarray = ['Someone Posted','Someone else posted'];
var i = 0;

function changeTitle(data){
  document.title = data;
}

function changeEvery5Seconds()
{
   i++;
   i = i%2;
   changeTitle(xarray[i]);
   setTimeout("changeEvery5Seconds();",5000);
}
changeEvery5Seconds();


来源:https://stackoverflow.com/questions/12491946/web-notifications-in-title-from-chatbox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!