开发者

webkitNotifications - SECURITY_ERR: DOM Exception 18 - script, OK - button

I followed http://www.beakkon.com/tutorial/html5/desktop-notification tutorial for html 5 desktop notifications. The demo on that page work for me. If i copy entire code it works so, but... when i call the method from javascript it don't display niether the notification or permision request. Instead it raises SECURITY_ERR: DOM Exception 18.

It seems the error is raised by the line which creates the notification itself.

Has anybody glue why button works and calling the function directly does not?


My current code:

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

function notif() {
  if (window.webkitNotifications.checkPermission() > 0) {
    RequestPer开发者_如何学运维mission(notif);
  }

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

Does not compute:

notif();

Computes:

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

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

WebKit: 534.13


SECURITY_ERR: DOM Exception 18 is valid if the user hasn't allowed your request to have notifications.

The reason why this is happening is simply because requestPermission is asynchronous. Once the user clicks on Allow, for permission to be granted, it will then allow you to use HTML5 notifications feature.

In your case, your not waiting for the user to click on Allow button, it is automatically trying to create the HTML5 notification without evening waiting for their confirmation. If you rearrange your conditionals, it should work.

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();
  }
}

As you notice above, place the notification creation in the conditional statement, when a callback gets fired it will be guaranteed to have permission.


I believe the createHtmlNotification accepts only one parameter, and that is to be a url to an HTML document.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜