javascript notification popup code example

Example 1: javascript to pop browser notification

<!doctype html>
	<head>
		<title>Show Notification using Javascript</title>
	</head>
	<body>
		<h3>Show Notification using Javascript</h3>


      <script>
      /* JS comes here */
      askForApproval();

      function askForApproval() {
        if(Notification.permission === "granted") {
          createNotification('Wow! This is great', 
                             'created by @study.tonight', 
                             'https://www.studytonight.com/css/resource.v2/icons/studytonight/st-icon-dark.png');
        }
        else {
          Notification.requestPermission(permission => {
            if(permission === 'granted') {
              createNotification('Wow! This is great', 'created by @study.tonight', 'https://www.studytonight.com/css/resource.v2/icons/studytonight/st-icon-dark.png');
            }
          });
        }
      }

      function createNotification(title, text, icon) {
        const noti = new Notification(title, {
          body: text,
          icon
        });
      }
      </script>
	</body>
</html>

Example 2: js alerts

3 types of JS Alerts

            - Information : You can only accept.
            - Confirmation: You can accept or decline.
            - Prompt    : You can accept, decline, and/or sendKeys.

Tags:

Html Example