问题
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