I have an audio element, which plays along with my alert box that shows up when a certain action happens. all of the pages on my website share the same JS file, and my JS file is in assets/js/core.js
, and my audio file is in assets/audio/error.mp3
. The audio only seems to play in index page, which is in the root directory of my website. all of the other pages are in /company/...
and company/legal/...
and so on. (the ...
represent the files that are in the directories.) I know that the page must first fetch the audio file, and the file path for each page would be different, so the JS would have to point to multiple directories.
This is the original JS code I had, which only plays in the index file:
// Create an audio element
let audio = new Audio("assets/audio/error.mp3");
audio.play(); // Play the audio
and an alternative I had tried to ake the audio play in all pages:
// Create an audio element
let audio = new Audio("assets/audio/error.mp3", "../audio/error.mp3", "../../assets/audio/error.mp3");
audio.play(); // Play the audio