It's pretty simple to get started really:

function playSound(url) {
    var a = new Audio(url);
    a.play();
}

Use that as whatever event handler you want for your application. Of course, I'd hope you'd want to do more than just play (for example, maybe pause would be good too?), but it's a start.

Answer from nickf on Stack Overflow
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › API › HTMLAudioElement › Audio
HTMLAudioElement: Audio() constructor - Web APIs | MDN
An optional string containing the URL of an audio file to be associated with the new audio element. A new HTMLAudioElement object, configured to be used for playing back the audio from the file specified by url. The new object's preload property is set to auto and its src property is set to ...
Discussions

How to play an audio file from an external url using Javascript? - Stack Overflow
I have an app (A) that makes an ajax request to a different app (B) to obtain a link for a wave sound file. Then, I want to use the link to play that sound file directly from my app (A). I tried to More on stackoverflow.com
🌐 stackoverflow.com
Play sound on hyperlink? - JavaScript - SitePoint Forums | Web Development & Design Community
Hi guys I’m using this script to play a sound when a link is pressed (I’m viewing it in an iPad): More on sitepoint.com
🌐 sitepoint.com
0
May 27, 2014
How to play MP3 audio with dynamic URL in HTML5 or JavaScript? - Stack Overflow
In JavaScript or HTML5, how to play MP3 audio with dynamic, rather than static, URL? Example - the following doesn't work: More on stackoverflow.com
🌐 stackoverflow.com
javascript - How to play audio? - Stack Overflow
I am making a game with HTML5 and JavaScript. How could I play game audio via JavaScript? More on stackoverflow.com
🌐 stackoverflow.com
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › HTML › Reference › Elements › audio
The Embed Audio element - HTML - MDN Web Docs - Mozilla
If a source fails (e.g., due to an invalid URL or unsupported format), the next source is attempted, and so on. An error event fires on the <audio> element after all sources have failed; error events are not fired on each individual <source> element. You can also use the Web Audio API to directly generate and manipulate audio streams from JavaScript code rather than streaming pre-existing audio files.
🌐
Go Make Things
gomakethings.com › how-to-play-a-sound-with-javascript
How to play a sound with JavaScript | Go Make Things
July 19, 2021 - Quick aside: autoplaying unwanted ... The new Audio() constructor lets you create a new HTMLAudioElement. Pass in the URL of the audio file as an argument....
🌐
Medium
mcasimirian.medium.com › playing-audio-on-click-in-your-javascript-app-72aa955068fc
Playing Audio on Click in Your JavaScript App | by Manon Sainton | Medium
May 12, 2021 - The function will trigger the play() method on a “click”, and will play sound clip from the url defined by the sound1 variable on line 2. ... Last, we should append the elements created to our original variable appBody. Like a stack of plates, playBtn has to append to playBtnDiv. Then playBtnDiv appends to appBody. Now refresh the page and click the button! ... I hope this article helped in adding some audio to your app!
Find elsewhere
🌐
Dobrian
dobrian.github.io › cmp › topics › sample-recording-and-playback-with-web-audio-api › 1.loading-and-playing-sound-files.html
Loading and Playing Sound Files
The onload function is a callback function that runs once the file has been grabbed from the server. Here, we are simply taking the raw audio file, and storing it in our AudioNode. const load = () => { const request = new XMLHttpRequest(); request.open("GET", "freejazz.wav"); request.responseType = "arraybuffer"; request.onload = function() { let undecodedAudio = request.response; audioCtx.decodeAudioData(undecodedAudio, (data) => buffer = data); }; request.send(); } Now, we can play the sound file by creating an AudioNode, attaching our buffer to it, connecting it to the dac, and playing it.
🌐
Flukeout
flukeout.github.io › simple-sounds
Play Sounds with JavaScript
<script src="sounds.js" type="text/javascript"></script> Then edit the sounds object to add your sounds... "dead" : { url : "sounds/dead.wav" }, "ping" : { url : "sounds/ping.mp3", volume : .5 } The name of the sound above will be "dead". You can add an optional volume property, which can range ...
🌐
SitePoint
sitepoint.com › javascript
Play sound on hyperlink? - JavaScript - SitePoint Forums | Web Development & Design Community
May 27, 2014 - Hi guys I’m using this script to play a sound when a link is pressed (I’m viewing it in an iPad): <!--[ In the head section of the HTML ]--> <!--audio--> <script type="text/javascript"> function play_single_sound() {…
🌐
javaspring
javaspring.net › blog › how-to-play-an-audio-file-from-an-external-url-using-javascript
How to Play an Audio File from an External URL Using JavaScript: Fixing NotSupportedError in Chrome & Firefox — javaspring.net
JavaScript provides two primary APIs for audio playback: the HTML5 Audio Element and the Web Audio API. For simple playback from a URL, the Audio Element is the most straightforward choice.
🌐
Mozilla
developer.mozilla.org › en-US › docs › Web › Media › Guides › Audio_and_video_delivery
Audio and video delivery - Media - MDN Web Docs - Mozilla
January 16, 2026 - Allowing JavaScript to generate streams facilitates a variety of use cases like adaptive streaming and time shifting live streams. Encrypted Media Extensions is a W3C proposal to extend HTMLMediaElement, providing APIs to control playback of protected content. The API supports use cases ranging from basic clear key decryption to high value video (given an appropriate user agent implementation).
🌐
Medium
noaheakin.medium.com › adding-sound-to-your-js-web-app-f6a0ca728984
Adding Sound to Your JS Web App. If you are looking to add another layer… | by Noah Eakin | Medium
November 3, 2020 - The above indicates that when our audio is called within our Javascript file it will begin playing in the browser automatically, loop back to the beginning of the audio clip indefinitely when finished, and create controls in the browser which allow the user to adjust volume, seeking, and other audio playback features.
🌐
Reddit
reddit.com › r/webdev › play an audio file without user interaction
r/webdev on Reddit: Play an Audio File WITHOUT User Interaction
August 23, 2023 -

Hello,

Im writing a wordpress plugin that monitors woocommerces shop_orders page. When a new order comes in the page is supposed to play a tada sound.

Everything is hooked into the shop_orders page in the admin. The idea is the user navigates to that page, every 30 seconds the page refreshes, during the refresh a filter is triggered and we check for the presence of a new order. This all works perfectly.

Now, when the new order is found, during this load we also output some javascript to play the notification sound:

if (($sounds) && (self::$new_order)) {

$plugin_dir = plugin_dir_url("mgd-woorestaurants-orderpickups/assets/") . "assets/tada.mp3";

echo "<script type='text/javascript'>let notifi= new Audio('$plugin_dir'); notifi.play();</script>";

}

I see the code in the page source, but no sound is every played. The console shows an error that reads:

"Uncaught (in promise) DOMException: play() failed because the user didn't interact with the document first."

I've read about the chrome policies for audio and video, and I cant seem to work out a way to get this audio file to play when a new order is found.

How do I set the browser to ignore its policies for this specific purpose?

Thanks in advance.

🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › API › HTMLAudioElement
HTMLAudioElement - Web APIs | MDN
April 28, 2025 - Creates and returns a new HTMLAudioElement object, optionally starting the process of loading an audio file into it if the file URL is given. No specific properties; inherits properties from its parent, HTMLMediaElement, and from HTMLElement. Inherits methods from its parent, HTMLMediaElement, ...
🌐
Delft Stack
delftstack.com › home › howto › javascript › play audio javascript
How to Play Audio Files in JavaScript | Delft Stack
February 2, 2024 - We can add audio files to our page simply by using the <audio> tag. It is the easiest way to play audio files without involving JavaScript at all. The src attribute of the <audio> tag specifies the audio file’s address.