本文介绍了webkitNotifications - SECURITY_ERR:DOM异常18 - 脚本,确定 - 按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遵循教程为html 5桌面通知。该页面上的演示适用于我。如果我复制整个代码,它的工作原理如此,但是...当我从javascript调用方法时,它不会显示通知或允许请求。相反,它会引发 SECURITY_ERR:DOM Exception 18



看起来错误是由创建通知本身的行提出的。



有没有人粘贴为什么按钮工作和直接调用函数不会?






我目前的代码:

 函数RequestPermission(callback)
{
window.webkitNotifications.requestPermission(callback);


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

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

不计算:

 通知符(); 

计算:

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

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

WebKit:534.13

解决方案

SECURITY_ERR:DOM Exception 18 是有效的,如果用户不允许你的请求有通知。



这种情况发生的原因很简单,因为 requestPermission 是异步的。一旦用户点击,授予权限,它将允许您使用HTML5通知功能。



在您的情况下,您不会等待用户点击按钮,它会自动尝试创建HTML5通知,无需等待确认。

 函数RequestPermission(callback){
window.webkitNotifications.requestPermission(回电话);


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






$ b

正如你在上面注意到的那样,将通知创建放置在条件语句,当一个回调被触发时,它将被保证有权限。


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

解决方案

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 , 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 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.

这篇关于webkitNotifications - SECURITY_ERR:DOM异常18 - 脚本,确定 - 按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 01:54
查看更多