Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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) {
    RequestPermission(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

like image 518
Mailo Světel Avatar asked Feb 06 '11 14:02

Mailo Světel


1 Answers

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.

like image 155
Mohamed Mansour Avatar answered Oct 02 '22 02:10

Mohamed Mansour