Gritter for jQuery (Growl) July 11, 2009 110 Comments
I’ve been hunting for a good jQuery Growl look-a-like plugin for some time, but I just can’t seem to find one that looks the exact same as the one for Mac OS, so like most web dabblers would do, I decided to roll my own.
For those who don’t know what Growl is, it’s basically a notification bubble/popup that appears in the top right of your desktop – examples can be found here. Think of it as calling the alert() function in JavaScript, only it’s not a modal, it’s subtle and it doesn’t look like dog shit.
Ruby on Rails user? Download the Gritter plugin for Ruby on Rails
The example above is also included in the download
Note: If you’re wanting to know how to integrate Gritter with CakePHP as a flash message, read this article. It’s in Spanish, I know, but if you’re slightly familiar with CakePHP, the code is what really matters.
This article has also been written in Belarusian
Usage
First, make sure the folder structure is in place (images/js/css).
Now add this to the head of your document:
<link rel="stylesheet" type="text/css" href="css/gritter.css" /> <script type="text/javascript" src="http://www.google.com/jsapi"></script> <script type="text/javascript">google.load('jquery', '1.5');</script> <script type="text/javascript" src="js/jquery.gritter.js"></script>
Adding a simple notification
$.gritter.add({ // (string | mandatory) the heading of the notification title: 'This is a notice!', // (string | mandatory) the text inside the notification text: 'This will fade out after a certain amount of time.' });
Adding a more complex notification
$.gritter.add({ // (string | mandatory) the heading of the notification title: 'This is a regular notice!', // (string | mandatory) the text inside the notification text: 'This will fade out after a certain amount of time.', // (string | optional) the image to display on the left image: 'http://a0.twimg.com/profile_images/59268975/jquery_avatar_bigger.png', // (bool | optional) if you want it to fade out on its own or just sit there sticky: false, // (int | optional) the time you want it to be alive for before fading out (milliseconds) time: 8000, // (string | optional) the class name you want to apply directly to the notification for custom styling class_name: 'my-class', // (function | optional) function called before it opens before_open: function(){ alert('I am a sticky called before it opens'); }, // (function | optional) function called after it opens after_open: function(e){ alert("I am a sticky called after it opens: \nI am passed the jQuery object for the created Gritter element...\n" + e); }, // (function | optional) function called before it closes before_close: function(e, manual_close){ // the manual_close param determined if they closed it by clicking the "x" alert("I am a sticky called before it closes: I am passed the jQuery object for the Gritter element... \n" + e); }, // (function | optional) function called after it closes after_close: function(){ alert('I am a sticky called after it closes'); } });
If you wanted to use the “sticky: true” option but still be able to delete it later, you can create a variable that will hold a unique identifier.
var unique_id = $.gritter.add({ // (string | mandatory) the heading of the notification title: 'This is a sticky notice!', // (string | mandatory) the text inside the notification text: 'This will not go away until the user has hit the (x) button because sticky has been set to true.', // (string | optional) the image to display on the left image: 'http://a0.twimg.com/profile_images/59268975/jquery_avatar_bigger.png', // (bool | optional) if you want it to fade out on its own or just sit there sticky: true });
Now to delete that specific notification you can call:
$.gritter.remove(unique_id, { fade: true, // optional speed: 'fast' // optional });
To remove all Gritter notifications you can call:
$.gritter.removeAll();
As well as
$.gritter.removeAll({ before_close: function(e){ alert("I am called before all notifications are closed. I am passed the jQuery object containing all of Gritter notifications.\n" + e); }, after_close: function(){ alert('I am called after everything has been closed.'); } });
If you want you can setup global defaults (optional)
Setting up global defaults is handy if you don’t want to specify a ‘time’ attribute for each $.gritter.add call.
$.extend($.gritter.options, { position: 'bottom-left', // defaults to 'top-right' but can be 'bottom-left', 'bottom-right', 'top-left', 'top-right' (added in 1.7.1) fade_in_speed: 'medium', // how fast notifications fade in (string or int) fade_out_speed: 2000, // how fast the notices fade out time: 6000 // hang on the screen for... });


Nice work & thanx for sharing.
You are great. Waited for something like this for months (years :) )
Your demo page shows a bug in Opera 9.64 (WinXP): hitting the "Remove all notifications" link will hide the notifications except any parts of them that are positioned lower than the bottom of the main content area (#container).
story has entered the popular today section on popurls.com...
Well, Chrome isn't in your list, and the notifications are not transparent in Chrome.
But nice idea! If Firefox would stop eating up half my computer's memory I'm sure I'd be able to see how nice it looks.
Excellent code mate...
thanks :)
Firefox 3.5 - Windows XP Pro SP3
Am I doing something wrong?
I'll implement in my projects, thanks for sharing
very nice and sexy work, thanks for sharing!
Firefox 3.5,
Chrome 2.0,
Safari 4.0,
Internet Explorer 8.0
Its always good to have choices :-)
However, if you roll over the box once the fade has *started*, it will then cancel the fade out and restore the box.
1 question: Is it possible to start gritter once the page is loaded.. like a "commercial", you come to the site, and once is loaded you see one gritter fading out after X seconds..?
Again thanks very much for your work and mood for sharing.
ANIMAL
@Thom: Ohhh! Yes, you're right.
@Beel: I will put in a fix for what Thom described.
@Animal: You can do something like $(document).ready(function(){ $.gritter.add({title: 'hi!', text: 'this is text'}); });
Is there a way to close the sticky notification, maybe from the notification itself or from a link elsewhere in the page?
Thanks again for this great plugin.
Very good work. Thanks for the fix!!
Thanks
I've tried several techniques using setInterval and jQuery's alternative but no luck.
Any help is appreciated
setInterval(function(){
if(check_new_message()){
$.gritter.add({ title: 'New Message!', text: 'You have a new message from Ross' });
}
}, 5000);
The check_new_message would be a function that makes an ajax call to check if there are new messages, it would return true or false.
i saw that question is not answered
thnx
---
When you hover over the message, the close button sometimes flashes visible/invisible. Is it possible to show the close button consistenly?
@stoffel:
Same problem here (ie7 trash)... When mouse hover in red areas (see image: http://img694.imageshack.us/img694/6698/jquerygritterhoverprobl.jpg) border and close button disapears, when mouse out the red areas border and close button became visible again. Thanks.
Reason first: if background of the div-container is transparent IE fires mouseleave Events on the regions that are no occupied by nontransparent inner elements.
Solution: Tell IE we have a background-image (that doesn't exist). Add this to the CSS
.gritter-item-wrapper {
[...]
background-image:url('.');
[...]
}
Thats it!
Thanks.
$(e).stop().css({ opacity: '' });
instead of:
$(e).stop().css({ opacity: 1 });
fixes another strange IE7+ behaviour and doesn't mess with other browser compatibility, I hope
Was wondering if I may possible ask as question. I have searched the blog and couldn't locate the answer so forgive me if this is a repeat. Is there a possible way to position the gritter in the center of the screen?
#gritter-notice-wrapper {
position:fixed;
top: 20px;
right: 20px;
width:301px;
z-index:9999;
}
thanks again for a great plugin
@stoffel & friends: I will also look into this IE7 issue and post an update when the fix has been applied.
Don't believe this is a wanted behaviour ;-)
Funny, my HTA-app (same IE engine) behaves different. There the Button fades with the frame. Really strange!
I have a question tho, hope you'll have time to answer it.
I'm using the same call to ID on a page, and only the first one works, any ideas?
It's when you add an item to a cart, it should trigger a gritter.add.
I use:
And on the next item, I use the same string. but it doesn't really work.
Any ideas? :)
Thanks again man.
http://store.distortedkids.com/
The "add to cart" links are the same.. yet, only the first one works :)
Thanks for the super fast reply dude!
$('#add-without-image').click(function(){ $.gritter.add({ // (string | mandatory) the heading of the notification title: 'Item Added!', // (string | mandatory) the text inside the notification text: 'Item has been added to your cart.' }); return false; });Change $('#add-without-image') to say $('.item_add'). Classes are for collections, ID's are unique identifiers that are only meant for 1 element. That should work :)
Any other issues, feel free to email me on my contact page.
Again, thanks a million, let me know if I can ever help you out with anything in return.
A jQuery plugin for creating Growl-like notifications (bubble/popup) that appears at the top right of your web pages.
......
Best of the web today!
Am I missing something?
$.gritter.add({ title: 'my title', text: 'your message' });Thank you for sharing it ..
How do I make it display a message automatically when a page loads up.
Thanks a lot for the hard work.
psssst to Arun: he answered that above. Put it inside a document ready call on the page: $(document).ready(function(){ $.gritter.add({title: 'hi!', text: 'this is text'}); });
One thing im a bit confused about.
Page loading.
Well you have answered that above and it works like you said it would.
Lets say a user leaves that page and then goes back to it.
In my test the message will only display once, the first time they go to that page.
That might be due to cookie or ajax, as the page is using ajax.
How would i make it so that every time the page is refreshed or gone back to that the message will display every time?
can you help me please
i try to show prompt with two state
i try to add to the second state overflow because the text inside is too long and I need it.
this is code
var statesdemo = {
state0: {html:’bbbbbbbbbbbbbbb’,
buttons: { Cancel: false, Next: true },
focus: 1,
submit:function(v,m,f){ calcule1();
if(!v) return true;
else
$.prompt.goToState(’state1′);
return false;
}
},
state1: {
html:”,
buttons: { Back: -1, Exit: 0 },
focus: 1,
submit:function(v,m,f){
if(v==0) $.prompt.close()
else if(v=-1)
$.prompt.goToState(’state0′);
return false;
}
}
};
$.prompt(statesdemo);
the result of calulate(); is too long and is affected to id=ss(second state)
Thank you………….
Thanks a lot Hat for pointing it out.
Gritter Rocks!
Thanks @ScaredyCat