Html5 / javascript desktop notifications

Started on the old forum in May 2012 · view the original in the archive · old links redirect here

#html5 #javascript #notifications

1 reply · 944 views · started 14 years ago · 1 min read

2 participants
something else Fellow · 2,296 posts #1

with html5 and a decent web browser you can now create desktop notifications... so even if the browser is not in focus you can show things like: you have 1 new message 😀

heres a Demo: http://something-else.wen.su/notification.html and here is the script:

<html>
<head>
<script type="text/javascript">

    //request permission
    function RequestPermission (callback)
    {
            window.webkitNotifications.requestPermission(callback);
    }

    //notify 
    function notification ()
    {
        if (!window.webkitNotifications)
        {
            alert('Get A Better Browser ;) Try Google Chrome');
            return;
        }
        if (window.webkitNotifications.checkPermission() > 0) {
            RequestPermission(notification);
        }
        var icon  = 'http://something-else.wen.su/icon.png';
        var title = 'New Message';
        var body   = 'You Have a new message :D';
        var popup = window.webkitNotifications.createNotification(icon, title, body);
        popup.show(); //open noticication
        setTimeout(function(){ //set timer
        popup.cancel(); //close noticication
        }, '15000'); //time of 15000 milliseconds
    }

    //html notify
    function htmlnotification ()
    {
        if (!window.webkitNotifications)
        {
            alert('Get A Better Browser ;) Try Google Chrome');
            return;
        }
        if (window.webkitNotifications.checkPermission() > 0) {
            RequestPermission(HTMLnotification);
        }
        var popup = window.webkitNotifications.createHTMLNotification('http://something-else.wen.su/');
        popup.show();  //open noticication
        setTimeout(function(){ //set timer
        popup.cancel(); //close popup
        }, '15000'); //time of 15000 milliseconds
    }

</script>
</head>
<body>
<button onclick="notification()">Create Notification</button><br/>
<button onclick="htmlnotification()">Create html Notification</button><br/>
</body>
</html>

Sign in to reply.

Similar topics

Keyboard shortcuts

g f
Go to forums
g h
Go to your feed
g m
Go to messages
r
Reply (in a topic)
/
Search
j
Next post
k
Previous post
?
This list
Ctrl/⌘ + Enter
Send what you are writing
Ctrl/⌘ + B / I
Bold / italic while writing
Esc
Close