Tuesday, June 2, 2015

A simple javascript notification

If you want to show a simple notification on the page using javascript and don't want to use any third-party librariy because of their overhead, here's what you can do: you can create a div, show and then remove it automatically.

  function showAlert(msg) {
    var el = document.createElement("div");
    el.setAttribute('style', 'position:fixed; top:50%; left:40%; background-color:white;');
    el.innerHTML = msg;
    setTimeout(function(){
      el.parentNode.removeChild(el);
    }, 1000);
    
    document.body.appendChild(el);
  }

That's a pretty light and easy solution which requires no third-party libraries.

No comments :

Post a Comment