Developer-Friendly Toast Alerts in JavaScript – Toast-JS
1. Include the library script ‘Toast.js’ on the webpage.
<script src="Toast.js"></script>
2. You need a container element where the toasts will appear.
<div id="toast-notification" class="toast-notification"></div>
3. Create a simple toast notification with the following code:
context: Target container.message: The text content of the toast.duration: How long the toast stays visible. Use Toast.LENGTH_SHORT (3 seconds), Toast.LENGTH_LONG (6 seconds), or provide milliseconds (e.g., 5000 for 5 seconds).Toast.makeText(document.body, "CSSScript", Toast.LENGTH_SHORT).show();
4. You can then chain methods after makeText() before calling show():
| Method | Description | Default |
|---|---|---|
setStyle() | Predefined or custom CSS object | STYLE_DEFAULT |
setPosition() | One of 12 screen positions | POSITION_BOTTOM_RIGHT |
setAnimation() | Entrance/exit animation pair | FADE |
setIcon() | URL or data URI for icon | None |
setDismissible() | Show close button | false |
setDuration() | Milliseconds to display | 3000 (LENGTH_SHORT) |
Available Styles:
Toast.STYLE_SUCCESS, Toast.STYLE_ERROR, Toast.STYLE_INFO, Toast.STYLE_WARNING, Toast.STYLE_DEFAULT_1, Toast.STYLE_DEFAULT_2, Toast.STYLE_LIGHT_MODE, etc. You can also pass the string name like "success".
Available Positions:
Toast.POSITION_TOP_LEFT, Toast.POSITION_TOP_CENTER, Toast.POSITION_TOP_RIGHT, Toast.POSITION_BOTTOM_LEFT, Toast.POSITION_BOTTOM_CENTER, Toast.POSITION_BOTTOM_RIGHT, Toast.POSITION_CENTER
5. You can use Toast-JS in React, but it’s not a typical React library. It directly manipulates the DOM outside of React’s virtual DOM.
import React from 'react';
function MyComponent() {
const handleShowToast = () => {
// Check if Toast is loaded (good practice)
if (window.Toast) {
const toast = Toast.makeText(
document.body, // Still use document.body
"Hello from React!",
Toast.LENGTH_SHORT
);
toast.setStyle(Toast.STYLE_INFO);
toast.setPosition(Toast.POSITION_BOTTOM_RIGHT);
toast.show();
} else {
console.error("Toast-JS library not loaded.");
}
};
return (
<button onClick={handleShowToast}>Show React Toast</button>
);
}
export default MyComponent; 03/14/2026
The post Developer-Friendly Toast Alerts in JavaScript – Toast-JS appeared first on CSS Script.
Jostling a folded piece of paper, holding it marooned in the air, selectman Beth Blair…
Boscawen voters cruised through a speedy town meeting Friday night, one with so little controversy…
Happy Saturday, all! This week, we found a number of deals that should help you…
Though it was weird to see the Golden Globes partner with Polymarket for its most…
Neo to the left of me. Pros are to the right. | Photo: Antonio G.…
Zendesk is to acquire Forethought AI. It says that this will be its largest acquisition…
This website uses cookies.