Introduction

In my recent MEAN stack project, I wanted to notify the end-user based on events. While researching further on this, I came across toastr.js. This library, I found to be very simple and easy to use. Importantly in today’s world of responsive sites, these notification are also responsive.

How to use toaster in short?

  • Add a reference to toastr.js from CDN or from the downloaded package.
  • Add a link to toastr.css and toaster-responsive.css to the HTML page.
  • In JavaScript, to generate toaster notification use below command,
toastr.success('Welcome to toaster notifications!', 'Toastr Notification').
This will show the following toaster notification on your Html page,
  • While displaying these notifications, the toaster library provides us various options e.g. Toastr Type as Success, Warning, Info, Error, etc. And positions can also be decided in a simplistic manner. You can refer to detailed toaster options at toastr.js.
  • Refer to the following section for the demonstration of toaster types and positions.
JSFiddle based sample code
If you want to just play with sample implementation, JSFiddle based implementation can be seen here.
Detailed Code Implementation goes as below,
  1. Create HTML page, toastrsample.html, and copy-paste below code to it.
    1. <!DOCTYPE html>
    2. <html>
    3. <head>
    4. <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    5. <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.4/jquery.min.js">
    6. </script>
    7. <scripttype="text/javascript" src="http://codeseven.github.com/toastr/toastr.js">
    8. </script>
    9. <link rel="stylesheet" type="text/css" href="http://codeseven.github.com/toastr/toastr.css">
    10. <link rel="stylesheet" type="text/css" href="http://codeseven.github.com/toastr/toastr-responsive.css">
    11. <script type="text/javascript" src="toastrsample.js">
    12. </script>
    13. <link rel="stylesheet" type="text/css" href="toastrsample.css">
    14. <title>How to Use Toastr</title>
    15. </head>
    16. <body>
    17. <h2>
    18. How to Use Toastr Notifications</h2>
    19. <div class="control-group" id="toastrTypeGroup">
    20. <div class="controls">
    21. <label>
    22. Toast Type</label>
    23. <label class="radio">
    24. <input type="radio" name="toasts" value="success" checked/>Success
    25. </label>
    26. <label class="radio">
    27. <input type="radio" name="toasts" value="info" />Info
    28. </label>
    29. <label class="radio">
    30. <input type="radio" name="toasts" value="warning" />Warning
    31. </label>
    32. <label class="radio">
    33. <input type="radio" name="toasts" value="error" />Error
    34. </label>
    35. </div>
    36. </div>
    37. <br/>
    38. <div class="control-group" id="toastrPositionGroup">
    39. <div class="controls">
    40. <label>
    41. Position</label>
    42. <label class="radio">
    43. <input type="radio" name="positions" value="toast-top-right" checked/>Top Right
    44. </label>
    45. <label class="radio">
    46. <input type="radio" name="positions" value="toast-bottom-right" />Bottom Right
    47. </label>
    48. <label class="radio">
    49. <input type="radio" name="positions" value="toast-bottom-left" />Bottom Left
    50. </label>
    51. <label class="radio">
    52. <input type="radio" name="positions" value="toast-top-left" />Top Left
    53. </label>
    54. <label class="radio">
    55. <input type="radio" name="positions" value="toast-top-full-width" />Top Full Width
    56. </label>
    57. <label class="radio">
    58. <input type="radio" name="positions" value="toast-bottom-full-width" />Bottom Full Width
    59. </label>
    60. <label class="radio">
    61. <input type="radio" name="positions" value="toast-top-center" />Top Center
    62. </label>
    63. <label class="radio">
    64. <input type="radio" name="positions" value="toast-bottom-center" />Bottom Center
    65. </label>
    66. </div>
    67. </div>
    68. <br/>
    69. <button type="button" class="btn" id="showtoast">
    70. Show Toastr Notification</button>
    71. </body>
    72. </html>
  2. Create JavaScript file (toastrsample.js) and add the following code,
    1. $(function()
    2. {
    3. $('#showtoast').click(function()
    4. {
    5. toastr.options =
    6. {
    7. "debug": false,
    8. "positionClass": $("#toastrPositionGroup input:radio:checked").val(),
    9. "onclick": null,
    10. "fadeIn": 300,
    11. "fadeOut": 100,
    12. "timeOut": 3000,
    13. "extendedTimeOut": 1000
    14. }
    15. var d = Date();
    16. toastr[$("#toastrTypeGroup input:radio:checked").val()](d, "Current Day & Time");
    17. });
    18. });
  3. Create Stylesheet file (toastrsample.css) and add the following code,
    1. body
    2. {
    3. font - family: Verdana;
    4. font - size: small;
    5. }
    After running the code and on click of button ‘Show Toastr Notification’, you should see toaster notifications appearing based on Toastr Type and Position you choose.
    Refer the following screenshot for the same,