我得到了这个非常奇怪的代码:没有调试,没有工作!我几乎疯了。
代码是在Chrome中显示通知。代码中有很多评论。
看看!!重要&& WEIRD !!,下一行是“console.log(_notification);”不能省略,如果确实如此,没有事件绑定可以工作。
虽然现在代码工作正常,但我只是好奇这个,为什么我不能删除“console.log(_notification);” ?
/**
* Notification
* @author: ijse
* @require: Chrome10+
* @params: same as webkitNotifications.create[HTML]Notification()
* @usage:
* new Notify("http://www.baidu.com").onshow(function() {
* alert("show");
* }).onclose(function() {
* alert("close");
* }).show();
*/
var Notify = function() {
var _params = arguments;
// Validate arguments
if(_params.length == 0) {
console.error("Notify need at least one argument");
return ;
}
// Check browser support
if(!window.webkitNotifications) {
console.error("Your browser does not support webkitNotifications feature!!");
return ;
}
var _onclose, _onclick, _onerror, _onshow;
var _notification, _replaceId, _showFlag = false;
function bindEvents() {
// Add event listeners
// In W3C, display event is called show
_notification.addEventListener("display", _onshow, false);
_notification.addEventListener("click", _onclick, false);
_notification.addEventListener("error", _onerror, false);
_notification.addEventListener("close", _onclose, false);
if(_replaceId)
_notification.replaceId = _replaceId;
// !!IMPORTANT&&WEIRD!! remove next line no events will work
console.log(_notification);
}
function createfn(permission) {
// About permission on Chrome:
// PERMISSION_ALLOWED (0) indicates that the user has granted permission to scripts with this origin to show notifications.
// PERMISSION_NOT_ALLOWED (1) indicates that the user has not taken an action regarding notifications for scripts from this origin.
// PERMISSION_DENIED (2) indicates that the user has explicitly blocked scripts with this origin from showing notifications.
if(permission == 0) {
// If permission is allowed
// Create notification
if(_params.length == 1)
_notification = window.webkitNotifications.createHTMLNotification(_params[0]);
else
_notification = window.webkitNotifications.createNotification(_params[0],_params[1],_params[2]);
// Bind events
console.log("bind event in createfn");
bindEvents();
// Show, if yes flag
!!_showFlag && _notification.show();
} else {
if(_onerror)
_onerror.call(this);
console.error("Notification permission is denied!!");
}
}
// If permission already allowed, do not require again
if(window.webkitNotifications.checkPermission() != 0) {
// Require permission from user
window.webkitNotifications.requestPermission(function() {
createfn.call(this, window.webkitNotifications.checkPermission());
});
} else {
createfn.call(this, window.webkitNotifications.checkPermission());
}
// Return handler methods
return {
onclose: function(fn) { _onclose = fn; console.log(1); return this; },
onclick: function(fn) { _onclick = fn; console.log(2); return this; },
onerror: function(fn) { _onerror = fn; console.log(3); return this; },
onshow : function(fn) { _onshow = fn; console.log(4); return this; },
show: function(replaceId) {
console.log("method show");
_replaceId = replaceId;
if(_notification) {
// Notification already been created
bindEvents();
_notification.show();
} else {
// Flag yes to show
_showFlag = true;
}
return _notification;
},
cancel: function() {
_notification.cancel();
}
} // return handler
}
new Notify("","Success!!", "Welcome to use empcy!!").onshow(function() {
var that = this;
window.setTimeout(function() { that.cancel(); }, 3000);
}).onclose(function() {
alert("close");
}).onclick(function() {
alert("clicked");
}).show("Welcome");
答案 0 :(得分:2)
我认为这可能是包围的问题。以下是您拥有的相关代码部分:
function bindEvents() {
// Add event listeners
// In W3C, display event is called show
_notification.addEventListener("display", _onshow, false);
_notification.addEventListener("click", _onclick, false);
_notification.addEventListener("error", _onerror, false);
_notification.addEventListener("close", _onclose, false);
if(_replaceId)
_notification.replaceId = _replaceId;
// !!IMPORTANT&&WEIRD!! remove next line no events will work
console.log(_notification);
}
如果删除console.log行,它将变为:
function bindEvents() {
// Add event listeners
// In W3C, display event is called show
_notification.addEventListener("display", _onshow, false);
_notification.addEventListener("click", _onclick, false);
_notification.addEventListener("error", _onerror, false);
_notification.addEventListener("close", _onclose, false);
if(_replaceId)
_notification.replaceId = _replaceId;
}
这可能会混淆JavaScript引擎,因为你有一个无括号的if语句,但后面跟一个右括号。您应该尝试在if语句中添加括号,如下所示:
function bindEvents() {
// Add event listeners
// In W3C, display event is called show
_notification.addEventListener("display", _onshow, false);
_notification.addEventListener("click", _onclick, false);
_notification.addEventListener("error", _onerror, false);
_notification.addEventListener("close", _onclose, false);
if(_replaceId) {
_notification.replaceId = _replaceId;
}
}
答案 1 :(得分:0)
也许您可以包含以下代码
https://github.com/h5bp/html5-boilerplate/blob/master/js/plugins.js
这是从html5-boilerplate项目中获取的,如果可以调用console.log
,则可以检查。
答案 2 :(得分:0)
最终,我没有解决这个问题,也找不到答案。我只是把那些代码放在一起,除了打印日志信息外它运行良好。