webkitNotifications - SECURITY_ERR:DOM例外18 - 脚本,确定 - 按钮

时间:2011-02-06 14:28:02

标签: html5 google-chrome notifications

我关注了html 5桌面通知的http://www.beakkon.com/tutorial/html5/desktop-notification教程。该页面上的演示适用于我。如果我复制整个代码它是这样的,但是......当我从javascript调用该方法时,它不会显示通知或permision请求。相反,它会提升SECURITY_ERR: DOM Exception 18

似乎错误是由创建通知本身的行引起的。

有人为什么按钮工作并且直接调用该函数没有?


我目前的代码:

function RequestPermission(callback)
{
  window.webkitNotifications.requestPermission(callback);
}

function notif() {
  if (window.webkitNotifications.checkPermission() > 0) {
    RequestPermission(notif);
  }

  notification = window.webkitNotifications.createHTMLNotification('http://localhost:3000/images/rails.png');
  notification.show();
}

不计算:

notif();

计算:

<button onclick="notif()">NOTIFY</button>

Google Chrome:9.0.597.84(Oficiálnísestavení72991)

WebKit:534.13

2 个答案:

答案 0 :(得分:8)

如果用户未允许您的请求收到通知,则

SECURITY_ERR: DOM Exception 18有效。

之所以发生这种情况,只是因为requestPermission是异步的。用户点击允许后,为了获得授权,它将允许您使用HTML5通知功能。

在您的情况下,您不等待用户点击允许按钮,它会自动尝试创建HTML5通知,而无需等待他们的确认。如果您重新安排条件,它应该可以工作。

function RequestPermission(callback) {
  window.webkitNotifications.requestPermission(callback);
}

function notif() {
  if (window.webkitNotifications.checkPermission() > 0) {
    RequestPermission(notif);
  } else {
    notification = window.webkitNotifications.createHTMLNotification('http://localhost:3000/images/rails.png');
    notification.show();
  }
}

如上所述,将通知创建放在条件语句中,当回调被触发时,它将保证具有权限。

答案 1 :(得分:0)

我相信createHtmlNotification只接受一个参数,就是这样 HTML文档的URL。

相关问题