JavaScript compatible with new and legacy Chrome and Firefox desktop notifications

Source: Internet
Author: User

1. New/older versions of Chrome and Firefox are supported, so settings are not supported in IE to blink the hint text at minimized windows.

2. Set the prompt window to show 5 seconds to close.

3. You can set the icon and click on the prompt window to jump to the page (see Input parameters).

            varTimer =NULL, title= $ (' title '). text (); $(' body '). On (' click ',function() {clearinterval (timer); $(' Title '). Text (title);                }); functionshowmsgnotification (title, MSG, ICONURL, Clickurl) {varNotification = window. Notification | | window.moznotification | |window.webkitnotification; if(window.webkitnotifications) {//Chrome old version                    if(window.webkitNotifications.checkPermission () = = 0) {                          varNotif =window.webkitNotifications.createNotification (ICONURL, title, MSG); Notif.display=function() {setTimeout (function() {notif.close (); }, 5000); } notif.onerror=function() {} Notif.onclose=function() {} Notif.onclick=function() {window.focus (); Window.location.href=Clickurl; } Notif.replaceid= ' meteoric ';                      Notif.show (); } Else{window.webkitNotifications.requestPermission ($jy. notify); }                  }                Else if(Notification) {//Support Desktop Notifications                    if(Notification.permission = = "Granted") {//notification has been allowed                        varInstance =NewNotification (title, {body:msg, Icon:iconurl //renotify:true                        }); Instance.onclick=function() {                              //$ (' body '). CSS ({' Background ': ' Red '});                             //console.log (' onclick ');Window.focus (); Window.location.href=Clickurl; //window.open (Clickurl, "_blank");                        }; Instance.onerror=function() {                              //console.log (' onerror ');                         }; Instance.onshow=function() {setTimeout (function() {instance.close (); }, 5000);                          }; Instance.onclose=function() {                              //console.log (' OnClose ');                         }; }Else{//The first time you ask or have forbidden the notification (if the user has previously suppressed the notification, then the browser will not ask the user again, the Notification.requestpermission () method is invalid)Notification.requestpermission (function(status) {if(Status = = = "Granted") {//user allows                                varInstance =NewNotification (title, {body:msg, Icon:icon                                    URL}); Instance.onclick=function() {window.focus (); Window.location.href=Clickurl;                                  }; Instance.onerror=function() {                                      //Something to do                                }; Instance.onshow=function() {                                      //Something to do                                }; Instance.onclose=function() {                                      //Something to do                                }; }Else{//User Forbidden                                return false;                      }                          }); }                  }Else{//not supported (ie, etc.)                    varindex = 0;                      Clearinterval (timer); Timer= SetInterval (function() {                          if(index%2) {                              $(' title '). Text (' "" ' + title ');//here is the Chinese full-width space, other not}Else {                              $(' title '). Text (' "flash" ' +title); } Index++; if(Index > 20) {clearinterval (timer); }                      }, 500); }              }  

JavaScript compatible with new and legacy Chrome and Firefox desktop notifications

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.