[英]web notifications in <title> from chatbox
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? 这是聊天的实时链接我是否必须篡改我的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 ! 在此先感谢您的帮助!
I'd suggest you to have a look at the JavaScript function setInterval()
. 我建议你看一下JavaScript函数setInterval()
。 Make this function check for updates every x ms, if it finds any updates; 如果发现任何更新,则使此功能每x ms检查一次更新; 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. 你肯定应该在PHP脚本上添加一些错误检查(检查变量是否设置: isset($_SESSION['value'])
),也可能在javascript上。
see "document.title" in javascript. 在javascript中查看“document.title”。
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();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.