所以我试着弄清楚这段JavaScript。我尝试做的事情是使用一个查询来显示我的数据库中的通知量。那么,假设我的数据库中有5条通知,是否可以在此代码中添加一个查询,使其显示如下内容:您好,您有5条看不见的消息?我已经在垃圾箱里找了几个小时了,简直要把我逼疯了。
提前感谢
<html>
<head>
<script language="javascript">
function notifyMe() {
// Let's check if the browser supports notifications
if (!("Notification" in window)) {
alert("This browser does not support desktop notification");
}
// Let's check if the user is okay to get some notification
else if (Notification.permission === "granted") {
// If it's okay let's create a notification
var notification = new Notification("Hi there!");
}
// Otherwise, we need to ask the user for permission
else if (Notification.permission !== 'denied') {
Notification.requestPermission(function (permission) {
// If the user is okay, let's create a notification
if (permission === "granted") {
var notification = new Notification("Hi there!");
}
});
}
// At last, if the user already denied any notification, and you
// want to be respectful there is no need to bother them any more.
}
</script>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<div><button onclick="notifyMe()">Meldingen ophalen</button></div>
</body>
</html>发布于 2015-04-21 17:51:29
要访问您的数据库,您需要PHP。
如果你想在不重新加载的情况下在网页上执行PHP,你必须像@Grimbode所说的那样使用"xmlhttprequest“。
这是一种通过Javascript从PHP函数获取PHP响应(在您的例子中,可以访问您的数据库)的方法。
为了让事情变得更简单,你应该看看$.post method from jQuery。
祝你的研究好运!
发布于 2015-04-21 21:36:57
这是一个脚本,它会每分钟调用一次,以检查是否有新的通知。
你的回复文本完全由你决定。你可以发回一个数字,你可以发回一个包含所有信息的json,等等。
setInterval(function () {
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
new Notification("You have " + xmlhttp.responseText + " notifications!");
}
}
xmlhttp.open("GET", "php_ajax_handler_url_here", true);
xmlhttp.send();
}, 60000);这需要你有php。每次调用ajax请求时,处理该请求的php文件都必须访问数据库并返回正确的响应。
https://stackoverflow.com/questions/29768362
复制相似问题