To play a notification sound on a website using JavaScript, you can use the HTML5 Audio element.
For example, we write:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Notification Sound</title>
</head>
<body>
<button onclick="playNotification()">Play Notification Sound</button>
<script>
function playNotification() {
var audio = new Audio('notification.mp3'); // Replace 'notification.mp3' with the path to your audio file
audio.play();
}
</script>
</body>
</html>
In this example, we have a button with an onclick attribute that calls the playNotification() function when clicked.
Inside the playNotification() function, we create a new Audio object and pass the path to the audio file (e.g., 'notification.mp3') as an argument.
Then, we call the play() method on the audio object to play the sound.
Make sure to replace 'notification.mp3' with the actual path to your audio file. You can use different audio formats like .mp3, .ogg, or .wav, depending on browser support and your preference.