【问题标题】:Jquery get Desktop Notification when page loaded页面加载时Jquery获取桌面通知
【发布时间】:2014-05-03 15:46:54
【问题描述】:

我使用此代码通过单击 div 在 chrome 上获取桌面通知

<script>
function notify() {
var havePermission = window.webkitNotifications.checkPermission();
if (havePermission == 0) {
  // 0 is PERMISSION_ALLOWED
   var notification = window.webkitNotifications.createNotification(
  'http://i.stack.imgur.com/dmHl0.png',
  'Chrome notification!',
  'Here is the notification text'
);

   notification.onclick = function () {
  window.open("http://stackoverflow.com/a/13328397/1269037");
  notification.close();
  }
  notification.show();
  } else {
  window.webkitNotifications.requestPermission();
 }
 }  
</script>

  <div style="width: 300px; height: 300px; background: yellow" onclick="notify()">
  Cick here to notify
  </div>

但是当我将onclick 更改为onload 时不起作用

即使使用click 事件或ready,我也在这段代码中使用jquery 这有什么问题?

【问题讨论】:

    标签: javascript jquery html google-chrome notifications


    【解决方案1】:

    我找到了答案 我用这个

    <script  type="text/javascript">
    var DesktopNotifications = {
    /**
     * Checks if notifications are supported
     * @return {Boolean}
     */
    isSupported:function() {
        return (window.webkitNotifications != 'undefined')
    },
    /**
     * ask use to display desktop notifications
     * @param callback
     */
    requestPermission:function(callbck) {
        window.webkitNotifications.requestPermission(function() {
            if (typeof(callbck) == "function") {
                callbck(window.webkitNotifications.checkPermission() == 0);
            }
        });
    },
    /**
     * display a notification
     * @param img full path of image to be displayed e.g. http://somedomain.com/photo.jpg
     * @param notitification_title title of notification
     * @param notification_body body of nitification
     * @return {Boolean}
     */
    doNotify:function(img,notitification_title,notification_body) {
        // permission is ok
        if (window.webkitNotifications.checkPermission() == 0) {
            window.webkitNotifications.createNotification(img, notitification_title, notification_body).show();
            return true;
        }
        return false;
    }
       }
    
     $(document).ready(function() {
        // request permission to display notifications 
        $("#permission_request").click(function() {
            DesktopNotifications.requestPermission();
        });
    
                // craete notification   
        $("#create_notification").ready(function() {
            if (!DesktopNotifications.doNotify("https://twimg0-   a.akamaihd.net/profile_images/2647445933/2c75afbe419bc7aaae71f01b29062b84_normal.jpeg", 
                                               "Desktop Notification Example", 
                                               "This is description of out Hello World notification example.")) {
                alert('Unable to fire notifications. Click "Request Permission" button to allow jaspreetchahal.org/examples access to send notifications to your desktop. Please note that this is just a demo and you can block this access anytime later');
            }
        });
    });
      </script>
    

    <a href="#requestnotifications" id="permission_request">Request permission</a>
    <a href="#createnotification" id="create_notification">Send</a>
    

    对我来说工作得很好

    【讨论】:

      猜你喜欢
      • 2011-07-08
      • 2018-11-04
      • 2018-12-07
      • 2016-06-10
      • 1970-01-01
      • 1970-01-01
      • 2016-06-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多