如何在单击 Webkit 通知时从浏览器中的任何其他选项卡移回当前选项卡
How to move back to the current tab from any other tab in browser when a Webkit Notification is clicked
我已经为聊天制作了一个Webkit桌面通知。 当用户不在当前聊天页面上(我的意思是在另一个选项卡上)时,会收到通知。一切正常,因为我希望它们:)
现在我需要做的是当用户单击浏览器中另一个选项卡上出现的通知时,它会将他移回聊天选项卡。
例如,用户在MyAwesomeChat.example.com
上聊天,然后移动到 Google.com 并在那里获得通知。只要他点击通知,它就会把他带到MyAwesomeChat.example.com
**
下面我用一个函数编写了我的代码。我在我想做上述事情的地方addEventListner
添加了一个"点击",尽管仍然毫无头绪。
desktopnotify:(chat) ->
if (window.webkitNotifications)
havePermission = window.webkitNotifications.checkPermission()
if havePermission is 0 && @window_blur
if (!(chat.get('USER_MAIL')==LOCAL_DATA.username))
# 0 is PERMISSION_ALLOWED means the user 'allow's the permission to show desktop notification when asked
pLength = Participants.where({USER_MAIL:chat.get('USER_MAIL')}).length
userImage = Participants.where({USER_MAIL:chat.get('USER_MAIL')})[pLength-1].get('USER_IMAGE')
if(userImage?)
notification = window.webkitNotifications.createNotification(userImage, chat.get('USER_MAIL'), chat.get('MESSAGE'))
else
notification = window.webkitNotifications.createNotification('/images/no-image.jpeg', chat.get('USER_MAIL'), chat.get('MESSAGE'))
notification.addEventListener('display',()->
window.setTimeout((e)->
notification.cancel()
, 5000)
notification.addEventListener('click',()->
console.log('here i want to do the magic. it will take back user to the page where he was chatting')
)
)
notification.show()
任何想法/帮助/建议将不胜感激。
默认情况下,通知与触发它的窗口/选项卡绑定。 因此,您只需要在单击时告诉它专注于绑定选项卡即可。
添加类似内容的内容
notification.onclick = function() {
window.focus(); // focus on binding window
this.cancel(); // close the notification on clicking it
};
所以你的最终代码应该看起来像
if (window.webkitNotifications)
havePermission = window.webkitNotifications.checkPermission()
if havePermission is 0 && @window_blur
if (!(chat.get('USER_MAIL')==LOCAL_DATA.username))
# 0 is PERMISSION_ALLOWED means the user 'allow's the permission to show desktop notification when asked
pLength = Participants.where({USER_MAIL:chat.get('USER_MAIL')}).length
userImage = Participants.where({USER_MAIL:chat.get('USER_MAIL')})[pLength-1].get('USER_IMAGE')
if(userImage?)
notification = window.webkitNotifications.createNotification(userImage, chat.get('USER_MAIL'), chat.get('MESSAGE'))
else
notification = window.webkitNotifications.createNotification('/images/no-image.jpeg', chat.get('USER_MAIL'), chat.get('MESSAGE'))
notification.addEventListener('display',()->
window.setTimeout((e)->
notification.cancel()
, 5000)
notification.addEventListener('click',()->
console.log('here i want to do the magic. it will take back user to the page where he was chatting')
)
)
//--------------------------------------------------------------------//
notification.onclick = function() {
window.focus();
this.cancel();
};
//--------------------------------------------------------------------//
notification.show()
干杯!!
相关文章:
- 尝试添加一个选项以使用append进行选择,但没有任何结果
- Select2js插件无法选择任何选项
- 如果使用jquery在页面加载时未选择任何选项,如何禁用下拉列表
- select2 使用 AJAX 加载数据不能选择任何选项
- 通过单击选项卡而不是单击网站的任何部分来隐藏选项卡
- 在 JavaScript 中识别浏览器选项卡的任何方法
- 如何填充下拉列表,从javascript中的另一个下拉列表中排除任何一个选项
- 我们是否有任何通用脚本/ jquery插件可以在通过ajax选项卡加载的内容上应用javascript
- 当“不相关”的 html 选择元素没有选择任何选项时,如何退出 jQuery 事件
- 选项卡中的a href链接没有执行任何操作
- javascript是否有一个选项可以使句点与任何字符匹配,包括换行符
- 为什么首先单击文档空白处的任何位置启动代码,而不是单击超链接,以及为什么打开了太多选项卡
- 有没有任何方法可以通过编程将浏览器选项卡放在前面(跨浏览器)
- 在这种javascript情况下,除了eval之外的任何其他选项
- 当教科书有任何值时,将自动选择下拉选项
- 确定是否已在 if 语句中选择任何选项
- 如果选择了所有无线电中的任何一个选项,则显示 Div
- 选择任何一个单选按钮后,单选按钮选项卡焦点无法正常工作
- AngularJs:选择在使用ng-repeat时不显示任何选项的下拉列表
- 如何在单击 Webkit 通知时从浏览器中的任何其他选项卡移回当前选项卡