Getting Started with EJ2 JavaScript Toast

14 Sep 20267 minutes to read

This section explains the steps required to create a simple Essential® JS 2 Toast control and demonstrates the basic usage of the Toast control in a JavaScript application.

Dependencies

The list of dependencies required to use the Toast component in an application is given below:

|-- @syncfusion/ej2-notifications
    |-- @syncfusion/ej2-base
    |-- @syncfusion/ej2-popups
    |-- @syncfusion/ej2-buttons

Setup for local environment

Refer to the following steps to set up the local environment.

Step 1: Create a root folder named my-app for the application.

Step 2: Create a my-app/resources folder to store local scripts and styles files.

Step 3: Open Visual Studio Code and create my-app/index.js and my-app/index.html files to initialize the Essential® JS 2 Toast control.

Control Initialization

The Essential® JS 2 JavaScript controls can be initialized by using below css styles and scripts

Add Syncfusion® Toast CSS Styles and Scripts

Include the following CSS and JavaScript files from CDN in the <head> section of the index.html file. The ej2-base, ej2-buttons, ej2-popups script is the required dependency package for the Toast component.

Styles(CSS):

https://unpkg.com/@syncfusion/ej2-bootstrap5.3-theme/styles/toast/index.css

Scripts (javascript):

https://cdn.syncfusion.com/ej2/35.1.37/ej2-base/dist/global/ej2-base.min.js
https://cdn.syncfusion.com/ej2/35.1.37/ej2-buttons/dist/global/ej2-buttons.min.js
https://cdn.syncfusion.com/ej2/35.1.37/ej2-popups/dist/global/ej2-popups.min.js
https://cdn.syncfusion.com/ej2/35.1.37/ej2-notifications/dist/global/ej2-notifications.min.js

Initialize the Toast using the show method

The Toast control is rendered by appending it to the target element and calling the show method to display the toast.

<!DOCTYPE html>
  <html xmlns="http://www.w3.org/1999/xhtml">
          <head>
               <title>Essential JS2 Toast</title>

               <!-- Essential JS 2 Toast's bootstrap5.3 theme -->
               <link href="https://unpkg.com/@syncfusion/ej2-bootstrap5.3-theme/styles/toast/index.css" rel="stylesheet" type="text/css" />

               <!-- Essential JS 2 Toast's global script -->
               <script src="https://cdn.syncfusion.com/ej2/ej2-base/dist/global/ej2-base.min.js"
                    type="text/javascript"></script>
               <script src="https://cdn.syncfusion.com/ej2/ej2-buttons/dist/global/ej2-buttons.min.js"
                    type="text/javascript"></script>
               <script src="https://cdn.syncfusion.com/ej2/ej2-popups/dist/global/ej2-popups.min.js"
                    type="text/javascript"></script>
               <script src="https://cdn.syncfusion.com/ej2/ej2-notifications/dist/global/ej2-notifications.min.js"
                    type="text/javascript"></script>
          </head>
       <body>
            <!-- Add the HTML <div> element  -->
             <div id="element"></div>
            <script>
               //Initialize Toast component
                var toast = new ej.notifications.Toast({
                    title: 'Matt sent you a friend request',
                    content: 'You have a new friend request yet to accept',
                    target: document.body
                });
               //Render initialized Toast component
                toast.appendTo('#element');
                toast.show();
            </script>
       </body>
  </html>

The output will be as follows:

In the previous example, the #element is the id of the HTML element in a page to which the toast is initialized.

ej.base.enableRipple(true);

//Initialize Toast component
var toast = new ej.notifications.Toast({
    title: 'Matt sent you a friend request',
    content: 'You have a new friend request yet to accept',
    target: document.body,
    timeOut: 0
});

//Render initialized Toast component
toast.appendTo('#element');
toast.show();
<!DOCTYPE html><html lang="en"><head>
    <title>Essential JS 2 Toast</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="Typescript Toolbar Controls">
    <meta name="author" content="Syncfusion">
    <link href="index.css" rel="stylesheet">
    <link href="https://unpkg.com/@syncfusion/ej2-bootstrap5.3-theme/styles/toast/index.css" rel="stylesheet" />
<script src="https://cdn.syncfusion.com/ej2/35.2.12/dist/ej2.min.js" type="text/javascript"></script>
</head>

<body>
    
    <div id="container">
        <div class="row"> <button class="e-btn" id="show_toast"> Show Toast</button> </div>
        <div id="element"></div>
        <br><br>
        <div id="result"></div>
        <div id="templateToast" style="display: none;color:red"> System affected by virus !!! </div>
    </div>


<script>
var ele = document.getElementById('container');
if(ele) {
  ele.style.visibility = "visible";
}   
      </script>
<script src="index.js" type="text/javascript"></script>
</body></html>

See Also