webkitNotifications - SECURITY_ERR:DOM 异常 18 - 脚本,确定 - 按钮 [英] webkitNotifications - SECURITY_ERR: DOM Exception 18 - script, OK - button

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

问题描述

我关注了 http://www.beakkon.com/tutorial/html5/desktop-通知 html 5 桌面通知教程.该页面上的演示对我有用.如果我复制整个代码,它就可以工作,但是...当我从 javascript 调用该方法时,它不会显示通知或权限请求.相反,它会引发 SECURITY_ERR: DOM Exception 18.

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?

我当前的代码:

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>

谷歌浏览器:9.0.597.84 (Oficiální sestavení 72991)

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

WebKit:534.13

WebKit: 534.13

推荐答案

SECURITY_ERR: DOM Exception 18 如果用户不允许您的请求有通知,则有效.

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

发生这种情况的原因仅仅是因为 requestPermission 是异步的.一旦用户点击 Allow,获得许可,它就会允许您使用 HTML5 通知功能.

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.

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

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.

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆