Toast In JavaScript

Toast In JavaScript

✪ Luminous Organisation

2nd August, 2021 AD



Toast is a kind of simplified popup message which appears for 2—4 seconds. For the purpose of application or website designing, toast is the users-side temporary debug or console log. It popups important message to users. JavaScript has alert() method, but using alert() instead of showing toast is messy at all. But toasting in JavaScript is not easy as there is no scope to toast directly. There is no toast method like alert() for toasting in JavaScript.

Any developer looking for a solution of toast method probably fails to find a better solution because blogs in websites about JavaScript Toast are totally uncompleted or complicated. However the given solution here is the best solution as we use line break after 20 characters. Write once, use anywhere this codes. HTML, CSS and JavaScript Codes are given below respectively with step by step explanation.

Use Test This Yourself to test your code.



HTML

Add this red code within body tag of your HTML once.

<div id="orgLuminous_snackbar">✪ Luminous Organisation</div>

CSS

Add the red code below within the style tag of your HTML once only.

#orgLuminous_snackbar {
  visibility: hidden;
  min-width: 250px;
  margin-left: -125px;
  background-color: #333;
  color: #fff;
  text-align: center;
  border-radius: 2px;
  padding: 16px;
  position: fixed;
  z-index: 1;
  left: 50%;
  bottom: 30px;
  font-size: 17px;
}

#orgLuminous_snackbar.orgLuminous_show {
  visibility: visible;
  -webkit-animation: fadein 0.5s, fadeout 0.5s 2.5s;
  animation: fadein 0.5s, fadeout 0.5s 2.5s;
}

@-webkit-keyframes fadein {
  from {bottom: 0; opacity: 0;} 
  to {bottom: 30px; opacity: 1;}
}

@keyframes fadein {
  from {bottom: 0; opacity: 0;}
  to {bottom: 30px; opacity: 1;}
}

@-webkit-keyframes fadeout {
  from {bottom: 30px; opacity: 1;} 
  to {bottom: 0; opacity: 0;}
}

@keyframes fadeout {
  from {bottom: 30px; opacity: 1;}
  to {bottom: 0; opacity: 0;}
}

JavaScript (i)

Add this red coded function within script tag.

function orgLuminous_Toast(myVal) {

var orgLuminous_nn = myVal.length /20;
if (orgLuminous_nn == Math.trunc(orgLuminous_nn)) {
orgLuminous_nn = Math.trunc(orgLuminous_nn);
} else {
orgLuminous_nn = Math.trunc(orgLuminous_nn) + 1;
}
var orgLuminous_sB = "";
for (var orgLuminous_ni = 0; orgLuminous_ni < orgLuminous_nn; orgLuminous_ni++) {
if ((orgLuminous_ni + 1) == orgLuminous_nn) {
orgLuminous_sB = orgLuminous_sB+myVal.slice(0, myVal.length)+"<br>";
} else {
orgLuminous_sB = orgLuminous_sB+ myVal.slice(0, 20)+"<br>";
myVal = myVal.slice(20, myVal.length);
}}
orgLuminous_sB = orgLuminous_sB.slice(0, (orgLuminous_sB.length)-4);
  document.getElementById("orgLuminous_snackbar").innerHTML = orgLuminous_sB;
  document.getElementById("orgLuminous_snackbar").className = "orgLuminous_show";
  setTimeout(function(){ document.getElementById("orgLuminous_snackbar").className = document.getElementById("orgLuminous_snackbar").className.replace("orgLuminous_show", ""); }, 3000);
}

JavaScript (ii)

Use this block wherever you want to Toast.

function orgLuminous_Toast(toastString);

The purple code above is changeable, replace it with your own variable or quoted value.

➤ Instance with variable:
var toastString = "Luminous Organisation";
function orgLuminous_Toast(toastString);

➤ Instance with quoted value:
function orgLuminous_Toast("Luminous Organisation");

Full Code Example

<!DOCTYPE html>
<html>
<head><title>Toast In JavaScript</title></head>
<body>
<input type="text" id="textId" value="" placeholder="Write popup message here">
<button onclick="orgLuminous_Toast(document.getElementById('textId').value);">Show Toast</button>
<div id="orgLuminous_snackbar">✪ Luminous Organisation</div>
<style>
#orgLuminous_snackbar {
  visibility: hidden;
  min-width: 250px;
  margin-left: -125px;
  background-color: #333;
  color: #fff;
  text-align: center;
  border-radius: 2px;
  padding: 16px;
  position: fixed;
  z-index: 1;
  left: 50%;
  bottom: 30px;
  font-size: 17px;
}

#orgLuminous_snackbar.orgLuminous_show {
  visibility: visible;
  -webkit-animation: fadein 0.5s, fadeout 0.5s 2.5s;
  animation: fadein 0.5s, fadeout 0.5s 2.5s;
}

@-webkit-keyframes fadein {
  from {bottom: 0; opacity: 0;} 
  to {bottom: 30px; opacity: 1;}
}

@keyframes fadein {
  from {bottom: 0; opacity: 0;}
  to {bottom: 30px; opacity: 1;}
}

@-webkit-keyframes fadeout {
  from {bottom: 30px; opacity: 1;} 
  to {bottom: 0; opacity: 0;}
}

@keyframes fadeout {
  from {bottom: 30px; opacity: 1;}
  to {bottom: 0; opacity: 0;}
}
</style>
<script>
function orgLuminous_Toast(myVal) {

var orgLuminous_nn = myVal.length /20;
if (orgLuminous_nn == Math.trunc(orgLuminous_nn)) {
orgLuminous_nn = Math.trunc(orgLuminous_nn);
} else {
orgLuminous_nn = Math.trunc(orgLuminous_nn) + 1;
}
var orgLuminous_sB = "";
for (var orgLuminous_ni = 0; orgLuminous_ni < orgLuminous_nn; orgLuminous_ni++) {
if ((orgLuminous_ni + 1) == orgLuminous_nn) {
orgLuminous_sB = orgLuminous_sB+myVal.slice(0, myVal.length)+"<br>";
} else {
orgLuminous_sB = orgLuminous_sB+ myVal.slice(0, 20)+"<br>";
myVal = myVal.slice(20, myVal.length);
}}
orgLuminous_sB = orgLuminous_sB.slice(0, (orgLuminous_sB.length)-4);
  document.getElementById("orgLuminous_snackbar").innerHTML = orgLuminous_sB;
  document.getElementById("orgLuminous_snackbar").className = "orgLuminous_show";
  setTimeout(function(){ document.getElementById("orgLuminous_snackbar").className = document.getElementById("orgLuminous_snackbar").className.replace("orgLuminous_show", ""); }, 3000);
}
</script>
</body>
</html>


Happy Implementation.




Hope you'll face no difficulties. Comment if you have any problem related with this code(s).

Comments

Popular posts from this blog

SeekBar JavaScript (I)

Image Editing / Element Filtering | JavaScript Block Editor

YouTube Embedding In HTML