WebkitNotifications - SECURITY_ERR: DOM 18 exception - script, OK button

I followed http://www.beakkon.com/tutorial/html5/desktop-notification for html 5 desktop notifications. The demo on this page works for me. If I copy all the code, it works like that, but ... when I call the method from javascript, it does not display a niether notification or permission request. Instead, it raises SECURITY_ERR: DOM Exception 18 .

It seems that the error is occurring in a line that itself creates a notification.

Has anyone stuck together why the button works and calls the function directly?


My current code is:

 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 calculate:

 notif(); 

Calculates:

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

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

WebKit: 534.13

+4
source share
2 answers

SECURITY_ERR: DOM Exception 18 valid if the user did not allow your request to receive notifications.

The reason this happens is simply because requestPermission is asynchronous. After the user clicks on Allow to get permission, he will allow you to use the HTML5 notification function.

In your case, without waiting for the user to click the Allow button, he automatically tries to create an HTML5 notification, without waiting for confirmation of his time. If you change your conventions, 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 noted above, place the creation of a notification in a conditional expression, when the callback is launched, it will be guaranteed permission.

+8
source

I believe that createHtmlNotification accepts only one parameter, and that should be the URL of the HTML document.

0
source

Source: https://habr.com/ru/post/1338633/


All Articles